6

I'm looking for an SQL formatter that will do this:

INSERT INTO MyTable (col1, col2, col3, col4)
VALUES (
   1, -- col1
   2, -- col2
   3, -- col3
   4  -- col4
)

I can't seem to find this feature in any of the free online SQL formatters, although it could be that I haven't looked hard enough - hence the question. Surely such a thing exists - or if not it looks simple enough that I'm tempted to try and write one myself...

For anyone unconvinced about the value of doing this here is one of the (many) actual inserts - very hard to tell what the values represent without the inline commenting (and having one value per line is useful too):

INSERT INTO ForecastAcqControl
(   ForecastImageServiceId, LayerId, Style, IsForecast, IsTiled,
    LatSW, LongSW, LatNE, LongNE, PixelsWidth, PixelsHeight, ZoomLevels,
    TimeCoverageHours, TimeStepMinutes, UpdateIntervalMinutes, CreatedDT
)
VALUES
(
    1,                  -- ForecastImageServiceId: OBSERVATIONS
    1,                  -- LayerId: RADAR_UK_Composite_Highres
    NULL,               -- Style
    FALSE,              -- IsForecast
    FALSE,              -- IsTiled
    47,                 -- LatSW
    -15,                -- LongSW
    61,                 -- LatNE
    5,                  -- LongNE
    1000,               -- PixelsWidth
    1200,               -- PixelsHeight
    4,                  -- ZoomLevels
    -2,                 -- TimeCoverageHours
    5,                  -- TimeStepMinutes
    5,                  -- UpdateIntervalMinutes
    UTC_TIMESTAMP()         -- CreatedDT
);
3
  • I use this SQL Formatter - it was loads of formatting options. Maybe it'll do what you're looking for.
    – Bridge
    Aug 8, 2012 at 11:03
  • Be careful with Oracle's SQLPLus and that commenting style. SQLPlus will ignore the whole line if it ends with a -- comment!
    – user330315
    Aug 8, 2012 at 12:00
  • 5 years later I've added this as an Issue for Poor Man's T-SQL Formatter... No promises, but I have recently started working on this tool again, so I might get to it, and otherwise I'm trying to lower the barrier to contribution as much as possible so someone else might... github.com/TaoK/PoorMansTSqlFormatter/issues/163
    – Tao
    Aug 9, 2017 at 20:10

3 Answers 3

2

Finally got round to creating a utility for doing this myself. (In retrospect, regular expressions may not have been the best way to go - but got there in the end!)

The tool is here: SQL Insert Commenter

Have done a fair amount of testing but there's bound to be something it doesn't work for so would be grateful if anyone trying it could let me know of any issues...

1
  • Pretty cool! the only obvious way I've found to "break" it is to add unbalanced/misleading parentheses inside some escaped fragment (string, name, etc): INSERT INTO t1 (col1) VALUES ((select '),('));
    – Tao
    Aug 9, 2017 at 20:22
1

I've never seen any user environment that would do such specific custom formatting.

You may be better off with a different syntax to accomplish a similar effect...

INSERT INTO
  ForecastAcqControl (
    ForecastImageServiceId, LayerId, Style, IsForecast, IsTiled, LatSW,
    LongSW, LatNE, LongNE, PixelsWidth, PixelsHeight, ZoomLevels, TimeCoverageHours,
    TimeStepMinutes, UpdateIntervalMinutes, CreatedDT
  ) 
SELECT
  ForecastImageServiceId =     1,
  LayerId                =     1,
  Style                  =  NULL,
  etc, etc

But you're still forced to adopt this practice manually.

5
  • Not sure if that syntax would work in MySQL. If I change it slightly and use AS instead of = for the column naming then it could be potentially confusing if for any reason the SELECT columns are not in the same order as the INSERT columns. E.g. see here: sqlfiddle.com/#!2/1780d/1/0 where I've changed col1 and col2 but the output is based on the order in the select, not the naming. For this reason I think I'd prefer to stick with the commented values. Aug 8, 2012 at 11:18
  • That is definitely not valid (standard) SQL. Which DBMS supports this?
    – user330315
    Aug 8, 2012 at 11:57
  • @a_horse_with_no_name - SQL Server certainly does, I think Oracle does, and I thought that MySQL does. I can't access SQLFiddle from this corporate network though, so I can't test that. Try testing the SELECT on it's own and see what you get.
    – MatBailie
    Aug 8, 2012 at 12:00
  • Oracle (and most probably all other DBMS) definitely does not. ForecastImageServiceId = 1 is not a valid expression for the select list (PostgreSQL would e.g. evaluate that as a boolean expression). The standard way to assign a column alias is to the use this 1 as ForecastImageServiceId
    – user330315
    Aug 8, 2012 at 12:01
  • @a_horse_with_no_name - SQL Server - www.sqlfiddle.com/#!3/d41d8/3193
    – MatBailie
    Aug 8, 2012 at 12:40
0

MSSQL allows to do this if you format like this:

INSERT INTO MyTable (col1, col2, col3, col4)
VALUES (
   1 --  col1
   ,2 -- col2
   ,3 -- col3
   ,4 -- col4
)

You can do above stuff in MySql in following way:

 INSERT INTO MyTable (col1,col2,col3,col4)
                  values('1'  #col1
                         ,'2' #col2
                         ,'3' #col3
                         ,'4' #col4
                        );

I have tested in sqlfiddle....Check out http://sqlfiddle.com/#!2/88eb8/1

3
  • Thanks for the reply, do you mean this is built into the MS SQL Server GUI tools as a formatting option? I'm using MySQL and unfortunately don't currently have access to SQL Server. Aug 8, 2012 at 10:40
  • Thanks but I think you may have misunderstood - I am looking for a formatting tool that will do this e.g. in the example above I could enter INSERT INTO MyTable (col1,col2,col3,col4) VALUES (1,2,3,4) as the input, click a button and get the formatted SQL with commented columns as the output. Aug 8, 2012 at 10:54
  • I think the OP wants the user environment to create the comments. As opposed to create the comments himself.
    – MatBailie
    Aug 8, 2012 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.