Friday, December 16, 2011

Inserting Multiple Records into a Table with a Single INSERT Statement

With SQL Server 2008 you no longer need to use multiple INSERT statements to insert multiple records.  Now with the new version of SQL Server you can use multiple VALUE clauses on a single INSERT statement.    This reduces the number of keystrokes needed to insert a number of different records.

 
-- Sample data
CREATE TABLE Vehicles (ID INT IDENTITY (1,1),
                       Name VARCHAR(100),
                       Manufacture VARCHAR(100));
-- insert multiple values in a single insert statement
INSERT INTO Vehicles VALUES
   ('Explorer','Ford'),
   ('Mustang','Ford'),
   ('Suburban','Chevrolet’),
   ('Metro','Geo');
-- Display Sample data                  
SELECT * FROM Vehicles;                      
-- Drop Sample Data
DROP TABLE Vehicles;

No comments:

Post a Comment