Insert Multiple Rows with One Insert Statement In SQL Server

Introduction:
This article explains how to insert multiple rows into table with single insert query in sql server or sql server insert multiple records in table with one insert statement with example.

To insert multiple rows into table with single insert query in sql server we can follow different methods like as shown below


Method- 1:

insert into @sampletable(id,name,education)
values(val1,val2,val3),
(val4,val5,val6),
(val7,val8,val9)

Example

declare @sampletable table(id int,name varchar(50),education varchar(50))

insert into @sampletable(id,name,education)
values(1,'Tom','MCA'),
(2,'Shaim','MBA'),
(3,'Jack','B.Tech')

select * from @sampletable

 Method- 2:

insert into @table1(id,name,education)
 select val1,val2,val3
union all
 select val4,val5,val6
union all
 select val7,val8,val9

Example

declare @sampletable table(id int,name varchar(50),education varchar(50))

insert into @sampletable(id,name,education)
select 1,'Tom','MCA'
union all
select 2,'Shaim','MBA'
union all
select 3,'Jack','B.Tech'

select * from @sampletable

Output:
Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment