SQL SERVER – 2008 – Insert Multiple Records Using One Insert Statement – Use of Row Constructor

I previously wrote article about SQL SERVER – Insert Multiple Records Using One Insert Statement – Use of UNION ALL. I am glad that in SQL Server 2008 we have new feature which will make our life much more easier. We will be able to insert multiple rows in SQL with using only one SELECT statement.

Previous method 1:
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('First',1);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Second',2);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Third',3);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Fourth',4);
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('Fifth',5);
GO

Previous method 2:
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO

SQL Server 2008 Method of Row Construction:
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES (
'First',1),
('Second',2),
('Third',3),
('Fourth',4),
('Fifth',5)

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts
Previous Post
SQLAuthority News – Microsoft Most Valuable Professional Award for SQL Server – MVP
Next Post
SQL SERVER – 2008 – Introduction to New Feature of Backup Compression

Related Posts

120 Comments. Leave new

  • Darshan shah
    July 2, 2008 6:25 pm

    Hi ,
    I want to update data into second server from first server ,without using linked server .
    I want to use openrowset.
    Can any one tell me how o do this ?

    Reply
    • Read about OPENROWSET in SQL Server help file
      It has example on how to communicate with different server

      Reply
  • Yah for another good 2008 feature, in addition to inline variable assignment

    Reply
  • Khushal patel
    July 3, 2008 9:21 am

    SQL Server 2008 ROCKS!!!

    Reply
  • Imran Mohammed
    July 3, 2008 11:13 am

    I always wanted SQL Server to accepts this kind of SQL Statements for INSERTS, finally they have come up with these…

    Thanks again.

    Reply
  • The syntax should be

    INSERT INTO MyTable (FirstCol, SecondCol)
    VALUES
    (‘First’,1),
    (‘Second‘,2),
    (‘Third‘,3),
    (‘Fourth‘,4),
    (‘Fifth‘,5)

    Reply
  • Wow! This is great. I cannot believe it’s taken this long for a feature that seems so natural.

    Thanks for the sharing!

    Scott

    Reply
  • Luke Phillips
    July 4, 2008 10:30 pm

    Errr, drel, the syntax *is* exactly as you describe …!

    Reply
  • that’s good for new generation because after upgrade on sql 2005 that is new version as discuss mr. drel as example
    ……………..
    so i appreciate to him.
    haing a nice job.

    Reply
  • There is an easier way to do this.
    If you are inserting a single row then use
    insert into(a1,b1) values(1,2)

    If inserting multiple rows using select statement use,

    Insert into(a1,b1) (select a1,b1 from c)

    Reply
  • im new to tsql and your blog is of big help to me, more power.

    Reply
  • Hi,
    Is below syntax works in sql server 2005 or it works only in sql server 2008?
    iam getting some error when i try to use it in sql server-2005

    INSERT INTO MyTable (FirstCol, SecondCol)
    VALUES (‘First’,1),
    (‘Second’,2),
    (‘Third’,3),
    (‘Fourth’,4),
    (‘Fifth’,5)

    Reply
    • This will work from version 2008 onwards only

      Reply
    • I tried it in SQL server 2008R2. It is not working. It is giving “Invalid column Name ‘firstcol’ “” error

      Reply
      • have you created table? use below

        Create table MyTable (FirstCol varchar(100), secondCol int);
        go

        INSERT INTO MyTable (FirstCol, SecondCol)
        VALUES (‘First’,1),
        (‘Second’,2),
        (‘Third’,3),
        (‘Fourth’,4),
        (‘Fifth’,5);
        go
        select * from MyTable;

  • can we submit multiple listbox value into multiple table with the one insert query

    Reply
  • @lakshmi: This is an MS SQL Server 2008 feature – you cannot use this with MS SQL Server 2005

    Reply
  • how to retrive deleted rows in sql server(in oracle having FLASHBACK QUERY),i know only we have an consistant backup that only restore the data.
    any other feature do u know pls tell me
    thanks and regards
    Rajasekar

    Reply
  • Hi Rajasekar,

    You can not get deleted Rows in Sql server.

    You can get it only in Trigger in “deleted table” or you can use “instead of trigger”

    Tejas

    Reply
  • can you give us the function tha inserts loads of rows at one time! PLEASE :)

    Reply
  • It´s a new feature of Sql 2008. Very Easy.

    Reply
  • I have a question about this new INSERT command in SQL Server 2008. Is it limited to only 1000 enteries? I have a lot more and I am getting the following error:
    The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values

    Reply
    • Yes. It is limited to 1000 entries. If you want to insert more than 1000 rows, move data to text file and use bulkinsert

      Reply
      • Bob Henkel (@TeamHenkel)
        July 23, 2012 9:35 pm

        I ran into this too and find this arbitrary value of 1000 to cause me to scratch my head. Did they roll dice to come up with this value or something? You shouldn’t have to put data in a file and use BCP to import say 1001 records. I know other databases that I have used this syntax with and it worked just fine with well over 1 million values with no issue.

  • Pederiko,
    I have tried this out and am getting the same error – so must be a limitation

    Reply
  • This is nice one but we can also insert more than one row as below:-

    Insert into tableName(col1,col2) values(1,2),(11,22),(44,55);

    Out Put
    Col1 Col2
    1 2
    11 22
    44 55

    Thanks………

    Reply

Leave a Reply