views:

258

answers:

6

Lets assume I have this long insert statement

insert into table1 (id, name, phone, very_long_col_name, ...) 
            values (1, 'very long name indeed...', '555-555-5555', 1, ...)

As you can see above, it gets hard to tell values from their column since their length is uneven

I'm looking for something (e.g. command line util) to format the above (not just SQL format) to this:

insert into table1 (id, name                      , phone         , very_long_col_name, ...) 
            values (1 , 'very long name indeed...', '555-555-5555', 1                 , ...)

This way I can see which value goes with which column easily

It can be a plugin to notepad++, a java utility, a plugin to an SQL IDE, what ever does the trick...

Prepared statements, T-SQL parameters, Hibernate, JPA etc is not an option right now

+6  A: 

Not suggesting a plugin, but I mostly see this kind of thing formatted this way:

insert into table1 
       (
         id, 
         name,
         phone, 
         very_long_col_name, 
         ...
       ) 
values 
       (
         1, 
         'very long name indeed...', 
         '555-555-5555', 
         1, 
         ...
       )

I find this more readable than scrolling through a very long line.

Oded
For a table with a lot of columns I'll also add a comment of the column name after each value. For example: 1, -- id
Tom H.
Since no better solution was suggested, this is the accepted answer.Thanks Oded...
Ehrann Mehdan
+1  A: 

Your better bet is to use SQL prepared statements. This lets you separate the SQL query syntax from your data, so you'd first prepare the statement:

$statement = mysqli_prepare("INSERT INTO `blah` (`id`,`phone`,`name`) VALUES(?,?,?)");

Then you bind the data to the statement:

$statement->bind('iss', 1234, "(555) 123-4567", "Kris");

I used PHP as an example, and the 'iss' in the above code says it's binding an Int and 2 strings in that order.

Kristopher Ives
+1  A: 

Have you considered the following alternative syntax?

INSERT INTO `table` SET
    `id` = 1,
    `name` = 'very long name indeed...',
    `phone` = '555-555-5555',
    `very_long_col_name` = 1,
    `...` = '...'
;
Alix Axel
This is not valid sql syntax for SQL-Server
Oded
Wish I could use it in non MySQL servers...
Ehrann Mehdan
@Oded: Since he didn't mentioned any specific DB I assumed he was using MySQL. Anyway, I tagged the question properly.
Alix Axel
+1  A: 

If the place that contains your SQL statement contains the DATA that you want to insert, then you are most probably doing something very, very, very wrong.

What do you want to achieve? Do you want to format the query, so that you can dump it in a pretty style for debugging purposes? Well, this is easy, just add strlen(some_string)-some_fixed_number number of whitespace at the appropriate places in your code. I can not suggest actual code here, because I do not know what language you use or what coding styles you prefer and so on...

But even if I wanted to, I do not see any value in this. You should separate SQL queries and the data that you use in your SQL queries (e.g. for inserting).

Building SQL query strings dynamically is out of fashion for some very good reasons (quoting, sql injection and so on...).

EDIT: If you want to format an SQL dump or some INSERT statements that prepare a database, then you can just use CSV formatted data. It is easier to read than SQL statements.

frunsi
For our code we use Hibernate rather than plain SQL (when we do, we use prepared statements at worse) but this is used for various reasons - e.g. db creation, mock data, development integration, etc.
Ehrann Mehdan
+1  A: 

Variables?

insert into
table1 ( id,  name,  phone,  very_long_col_name, ...) 
values (@id, @name, @phone, @long_val, ...)

(obviously you need to declare and set / select these too)

Marc Gravell
Nice approach, but please see my comment to frunsi, the current structure of the query is given, I can't change it, I just want to see it clearly.
Ehrann Mehdan
+1  A: 

In Oracle (since we can select from dual) I like to make these into insert into select from so I can alias the columns and make it easier to read:

insert into table1 
(
 id, 
 name, 
 phone, 
 very_long_col_name,
 ...
) 
select 1 id, 
       'very long name indeed...' name, 
       '555-555-5555' phone, 
       1 very_long_col_name, 
       ...
  from dual;       
Dougman