SQL Server - Insert Multiple Records Using One Insert Statement –> (Word) SQL Server - Insert Multiple Records Using One Insert Statement –> (PDF) SQL Server - Insert Multiple Records Using One Insert Statement –> (Epub) SQL Server - Insert Multiple Records Using One Insert Statement –> (Text)
SQL Server - Insert Multiple Records Using One Insert Statement –> (XML) SQL Server - Insert Multiple Records Using One Insert Statement –> (OpenOffice) SQL Server - Insert Multiple Records Using One Insert Statement –> (XPS)
This article has been published 2 years, 155 days ago
The estimated time to read this article is 3 minutes

This is very interesting question I have received from new developer. How can I insert multiple values in table using only one insert? Now this is interesting question. When there are multiple records are to be inserted in the table following is the common way using T-SQL.

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

The clause INSERT INTO is repeated multiple times. Many times DBA copy and paste it to save time. There is another alternative to this, which I use frequently. I use UNION ALL and INSERT INTOSELECT… clauses. Regarding performance there is not much difference.

If there is performance difference it does not matter as I use this for one time insert [gs script]. I enjoy writing this way, as it keeps me focus on task, instead of copy paste. I have explained following script to new developer. He was quite pleased.

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

The effective result is same.

SOURCE

LINK (Sqlauthority.com)

LANGUAGE
ENGLISH
SQL Server - Insert Multiple Records Using One Insert Statement SQL Server - Insert Multiple Records Using One Insert Statement
Facebook Twitter Email Plusone Linkedin Stumbleupon Delicious Digg
Add Comment Register



   
© 2011 - 2013 HeelpBook - Today is: 22/8/2014 Last updated: April 2, 2014 at 19:54 pm Get helped -