Friday, April 26, 2013

SQL Server Insert Multiple Records

SQL Server Insert Multiple Records

SQL Server INSERT DML operation allows user to add multiple records in one statement.

 

Syntax:

insert into [tablename]  values (),(),()

   or

insert into [table1]  select * from [table2]


for ex: Create a Sports Table

CREATE TABLE [dbo].[Sports] (
    [Id]      INT          IDENTITY (1, 1) NOT NULL,
    [Name]    VARCHAR (20) NOT NULL,
    [Country] NCHAR (2)    NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);


Insert multiple records into Sports table 



insert into sports values
 (N'Tennis', N'IN'),
 (N'Hockey', N'IN'),
 (N'Football', N'IN'),
 (N'BaseBall', N'US'),
 (N'Hand FoodBall', N'US'),
 (N'Cricket', N'IN'),
 (N'Cricket', N'UK'),
 (N'Basket Ball', N'UK'),


Select * from sports

IDNameCountry
23 Tennis IN
24 Hockey IN
25 Football IN
26 BaseBall US
27 Hand FoodBall US
28 Cricket IN
29 Cricket UK
30 Basket Ball UK


METHOD 2:  Inserting multiple records from another table(s).


insert into [table 1] select * from [table2]  etc.,






Tags: SQL Server Insert Multiple Records, Inserting multiple records using INSERT statement, Inserting multiple records using Select statement, Inserting bulk rows into table, using INSERT command inserting multiple records.

No comments:

Post a Comment