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 Statement

Jul 31, 2016
Introduction:

Here I will explain how to insert multiple rows with one insert statement in 
sql server or insert multiple multiple row values with stored procedure in sql server or insert multiple rows from select statement in sql server or insert multiple rows into table at once in sql server or sql server insert multiple rows from another table 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 or select statement in sql server.

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

SQL Server Insert Multiple Rows At Once

Following is the syntax of inserting multiple rows with one statement in sql server.


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

SQL Server Insert Multiple Rows from Select Statement

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


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

4 comments :

Anonymous said...

Hi
Can you please upload bulkUPS for multiple insert
as I want to know how can I insert more tha 25K+ records from excel

Anonymous said...

rfthtr

Anonymous said...

Test

Unknown said...

Hi function can return more than one value.
yes? how

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.