Search This Blog

Inserting multiple Rows in a Single Insert Query in SQL 2008

Inserting multiple Rows in a Single Insert Query in SQL 2008 

Many times you have requirement to insert many Rows at once in a table, so its a best to use Multi-row values clause in Insert Statements which was added in sql 2008.

Below is the Sample table using Multi row values clause in Insert Statement.

create table Employee1
(Empid int identity (1,1) ,EmpName nvarchar(50),EmpSalary float)

--This will not work in sql 2005
insert into Employee1 values('Amit','5000')
,('Sumit','6000')
,('Raj','8000')
,('vijay','9000')
,('suresh','10000')


--In SQL 2005

insert into Employee1 (EmpName ,EmpSalary )
Select 'Sumit','6000'
union all
Select 'Raj','8000'
union all
select 'vijay','9000'
union all
select 'suresh','10000'

--Simple Insert Statement with multiple Insert query

insert into Employee1 values('Amit','5000')
insert into Employee1 values('Sumit','6000')
insert into Employee1 values('Raj','8000')





No comments:

Post a Comment