hmm this is interesting question
How can I insert multiple values in table using only one insert statement?
Normally when there are multiple records are to be inserted in the table we use following way in T-SQL.
USE TestDB
GO
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (1, 'TestVal1' ,'TestDesc1');
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (2, 'TestVal2' ,'TestDesc2');
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (3, 'TestVal3' ,'TestDesc3');
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (4, 'TestVal4' ,'TestDesc4');
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (5, 'TestVal5' ,'TestDesc5');
GO
The clause INSERT INTO is repeated multiple times.
Many times DBA/Programmer copy and paste it to save time.
There is another alternative to this, which is easy to use without repetition of INSERT Clause.
For this use UNION ALL and INSERT INTO … SELECT… clauses.
I enjoy writing this way, as it keeps me focus on task, instead of copy paste of statement.
The effective result is same with less writing.
Regarding performance UNION ALL and INSERT INTO … SELECT… clauses performs better than normal Multiple INSERT Statement
USE TestDB
GO
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
SELECT 1, 'TestVal1' ,'TestDesc1'
UNION ALL
SELECT 2, 'TestVal2' ,'TestDesc2'
UNION ALL
SELECT 3, 'TestVal3' ,'TestDesc3'
UNION ALL
SELECT 4, 'TestVal4' ,'TestDesc4'
UNION ALL
SELECT 5, 'TestVal5' ,'TestDesc5'
GO
Cheers!!!!!!!!!!!!!!!!!!!!!!
Sandyyy
How can I insert multiple values in table using only one insert statement?
Normally when there are multiple records are to be inserted in the table we use following way in T-SQL.
USE TestDB
GO
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (1, 'TestVal1' ,'TestDesc1');
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (2, 'TestVal2' ,'TestDesc2');
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (3, 'TestVal3' ,'TestDesc3');
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (4, 'TestVal4' ,'TestDesc4');
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
VALUES (5, 'TestVal5' ,'TestDesc5');
GO
The clause INSERT INTO is repeated multiple times.
Many times DBA/Programmer copy and paste it to save time.
There is another alternative to this, which is easy to use without repetition of INSERT Clause.
For this use UNION ALL and INSERT INTO … SELECT… clauses.
I enjoy writing this way, as it keeps me focus on task, instead of copy paste of statement.
The effective result is same with less writing.
Regarding performance UNION ALL and INSERT INTO … SELECT… clauses performs better than normal Multiple INSERT Statement
USE TestDB
GO
INSERT INTO TestTable (TestColId, TestColTitle,TestColDesc)
SELECT 1, 'TestVal1' ,'TestDesc1'
UNION ALL
SELECT 2, 'TestVal2' ,'TestDesc2'
UNION ALL
SELECT 3, 'TestVal3' ,'TestDesc3'
UNION ALL
SELECT 4, 'TestVal4' ,'TestDesc4'
UNION ALL
SELECT 5, 'TestVal5' ,'TestDesc5'
GO
Cheers!!!!!!!!!!!!!!!!!!!!!!
Sandyyy
Comments
Post a Comment