How do i insert multiple rows in database. for example: I have 3 list: list1: a1,a2,a3,a4 list2: b1,b2,b3,b4 list3: c1,c2,c3,c4
I want to insert a1,b1,c1 in 1st row. a2,b2,c2 in 2nd row,... How do I write one sql insert statement to do that?
i'd imagine making a stored procedure to deal with the inserts would be the easiest way.
you can modify this sp to scroll through the list and put the whole lot of your list into variables then just insert 'em.
hope this helps
/*******************************************************************************
Description Delimit a string and return specified segment
Author: Peter Yates
Example usage: exec delimiter 'te~st~in~g1~23', '~', 5
Modifications:
*******************************************************************************/
if exists (select *
from dbo.sysobjects
where id = object_id(N'[dbo].[delimiter]')
and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[delimiter]
go
create procedure delimiter(
@str nvarchar (4000), --delimited string
@del nvarchar (10), --delimiter
@sect int --section of string wanted
)
as
begin
declare @nextstr nvarchar(4000)
declare @pos int
declare @nextpos int
create table #valuetable (id int identity, value varchar(500))
set @nextstr = ''
set @str = @str + @del
set @pos = charindex(@del,@str)
set @nextpos = 1
while (@pos <> 0)
begin
set @nextstr = substring(@str,1,@pos - 1)
insert into #valuetable
([value])
values
(@nextstr)
set @str = substring(@str,@pos +1,len(@str))
set @nextpos = @pos
set @pos = charindex(@del,@str)
end
select value
from #valuetable
where id = @sect
return
end