Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write INSERT statements with values next to column names?

When writing an INSERT statement with a lot of columns, it would be nice to have the value next to the column name like in an UPDATE statement. Something like:

insert into myTable
set
  [col1] = 'xxx',
  [col2] = 'yyy',
  [col3] = 42
  etc...

Are there any tricks to mimic this?

I thought I was onto something with this:

insert into myTable
select
  [col1] = 'xxx',
  [col2] = 'yyy',
  [col3] = 42
  etc...

But the aliases aren't actually being associated with the insert table's columns and if someone added a new column to the table it could really screw things up. Anyone have any other ideas of how one could do this?

like image 366
adam0101 Avatar asked Jan 04 '11 15:01

adam0101


People also ask

Which statement can you use to add values to a column?

The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.

What is insert statements?

The INSERT INTO statement is used to insert new records in a table.

What statement is used to insert values into a table?

The insert statement is used to insert or add a row of data into the table.


2 Answers

The closest you'll get would be to specify the columns on the insert (this will protect you from your concern about a new column being added) and alias the values on the select (which gives you some degree of self-documenting code).

insert into myTable
    ([col1], [col2], [col3])
    select 'xxx' as [col1], 'yyy' as [col2], 42 as [col3]
like image 150
Joe Stefanelli Avatar answered Nov 01 '22 05:11

Joe Stefanelli


For big inserts, I've done fancy (and perhaps overfly fussy) layouts. Some examples:

INSERT MyTable ( MyTableId,        Name,             Description,      SomeStringData1
                ,SomeStringData2,  SomeStringData3,  SomeStringData4,  MoreStringData1
                ,MoreStringData2,  MoreStringData3,  MoreStringData4,  SomeNumericData1
                ,SomeNumericData2, SomeNumericData3, SomeNumericData4, MoreNumericData1
                ,MoreNumericData2, MoreNumericData3, MoreNumericData4, BigBlobAA
                ,BigBlobBB,        EnteredAtDate,    UpdatedAtDate,    RevisedAtDate
                ,NeedAnotherDate )
 values
               ( @MyTableId,        @Name,             @Description,      @SomeStringData1
                ,@SomeStringData2,  @SomeStringData3,  @SomeStringData4,  @MoreStringData1
                ,@MoreStringData2,  @MoreStringData3,  @MoreStringData4,  @SomeNumericData1
                ,@SomeNumericData2, @SomeNumericData3, @SomeNumericData4, @MoreNumericData1
                ,@MoreNumericData2, @MoreNumericData3, @MoreNumericData4, @BigBlobAA
                ,@BigBlobBB,        @EnteredAtDate,    @UpdatedAtDate,    @RevisedAtDate
                ,@NeedAnotherDate )

This works if you're pretty darn certain that you wont ever be inserting columns or otherwise modifying what is being inserted. It gets gets everything on one screen, and makes it fairly simple to pick out what value goes into which column.

If inserted values are likely to change or are complex (such as case statements), I do the following (outdent all but every fifth item):

INSERT MyTable
  (
     MyTableId
    ,Name
    ,Description
    ,SomeStringData1
   ,SomeStringData2
    ,SomeStringData3
    ,SomeStringData4
    ,MoreStringData1
    ,MoreStringData2
   ,MoreStringData3
    ,MoreStringData4
    ,SomeNumericData1
    ,SomeNumericData2
    ,SomeNumericData3
   ,SomeNumericData4
    ,MoreNumericData1
    ,MoreNumericData2
    ,MoreNumericData3
    ,MoreNumericData4
   ,BigBlobAA
    ,BigBlobBB
    ,EnteredAtDate
    ,UpdatedAtDate
    ,RevisedAtDate
   ,NeedAnotherDate
  )
 values
  (
     MyTableId
    ,Name
    ,Description
    ,SomeStringData1
   ,SomeStringData2
    ,SomeStringData3
    ,SomeStringData4
    ,MoreStringData1
    ,MoreStringData2
   ,MoreStringData3
    ,MoreStringData4
    ,case
       when something then 'A'
       when orOther   then 'B'
       else 'Z'
     end
    ,SomeNumericData2
   ,SomeNumericData3
    ,SomeNumericData4
    ,MoreNumericData1
    ,MoreNumericData2
    ,MoreNumericData3
   ,MoreNumericData4
    ,BigBlobAA
    ,BigBlobBB
    ,EnteredAtDate
    ,UpdatedAtDate
   ,RevisedAtDate
    ,NeedAnotherDate
  )

(After adding that CASE statement, I "counted indents" to make sure I had everything lined up properly.)

It takes a bit of effort to get things layed out properly, but it can make maintenance, support, and subsequent modification simpler.

like image 1
Philip Kelley Avatar answered Nov 01 '22 07:11

Philip Kelley