Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server - Insert Multiple Rows with One Insert Statement

Jul 27, 2014
Introduction:

Here I will explain 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.
Description:
In previous articles I explained SQL Server Get total rows count in union query, SQL Server Difference between Union and Union All, SQL Server Replace multiple spaces in string with single space, SQL Server Remove html tags from string and many articles relating to SQL server. Now I will explain how to insert multiple rows into table with single insert query in sql server.

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

Method1:


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
Method2:


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


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

Maitreya said...

Thanks! i didn't knew that!

Unknown said...

good example

Rajkumar Palnati said...

Hi Bro, good example
which one is good for inserting huge data.

Give your Valuable Comments

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

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.