Tuesday, 24 January 2017

How do I insert multiple rows WITHOUT repeating the “INSERT INTO dbo.Blah” part of the statement?

First Option
Syntax works in SQL Server 2008 (but not in SQL Server 2005)
CREATE TABLE MyTable (id int, name char(10));

INSERT INTO MyTable (id, name) VALUES (1, 'Bob'), (2, 'Peter'), (3, 'Joe');

SELECT * FROM MyTable;

id |  name
---+---------
1  |  Bob       
2  |  Peter     
3  |  Joe   
Second Option
INSERT INTO dbo.MyTable (ID, Name) 
select * from
(
 select 123, 'Timmy'
  union all
 select 124, 'Jonny' 
  union all
 select 125, 'Sally'
 ...
) x    

No comments:

Post a Comment