Friday, August 1, 2014

Insert into Multiple rows in single query: SQL

Method 1 - Traditional InsertINSERT INTO #SQLAuthority (IDValue)VALUES (1'First');INSERT INTO #SQLAuthority (IDValue)VALUES (2'Second');INSERT INTO #SQLAuthority (IDValue)VALUES (3'Third');

TRUNCATE TABLE #SQLAuthority;
Method 2: INSERT…SELECT
INSERT INTO #SQLAuthority (IDValue)
SELECT 1'First'UNION ALLSELECT 2'Second'UNION ALLSELECT 3'Third';

TRUNCATE TABLE #SQLAuthority;
Method 3: SQL Server 2008+ Row Construction
-- Method 3 - SQL Server 2008+ Row ConstructionINSERT INTO #SQLAuthority (IDValue)VALUES (1'First'), (2'Second'), (3'Third');

No comments:

Post a Comment