HI WELCOME TO KANSIRIS

SQL Server Insert Multiple Rows from Select Statement

Leave a Comment

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

Example:


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

insert into @table1(id,name,education)
values(1,'suresh','b.tech'),
(2,'rohini','msc'),
(3,'praveen','btech')


select * from @table1

Following is the syntax of inserting multiple rows into table using select statement in sql server.


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

Example:


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

insert into @table1(id,name,education)
select 1,'suresh','b.tech'
union all
select 2,'rohini','msc'
union all
select 3,'praveen','btech'

select * from @table1
When we run above query it will return all inserted records with single query like as shown below

Output


0 comments:

Post a Comment

Note: only a member of this blog may post a comment.