Tuesday, May 18, 2010

How to Insert Multiple Rows in SQL Server

To insert multiple rows with the help of single query you can use following syntax. Regarding performance there is not much difference. If there is performance difference it does not matter as we can use this for one time insert script.


INSERT INTO Table1 (FirstColumn, SecondColumn)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5


Or
Insert
  Into daTable
     ( col1, col2, ... coln )
values
     ( '21',   9,  ... 'oh' )
    ,( '22',  37,  ... 'my' )
    ,( '23',  42,  ... 'we' )
    ,( '24',  11,  ... 'be' )
    ,( '25',  77,  ... 'at' );

No comments:

Followers

Link