Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
csri
Starting Member
45 Posts |
Posted - 2008-07-17 : 15:06:15
|
| Dear All I want to Insert values into multiple rows of a table using stored procedure in sql server 2005.Table name is Response.columns are as follows(ResponseId int,queryid int ,commands varchar)commandIds can have more than 1 value.My stored procedure isCreate procedure InsertCommandvalues(@p_ResponseId int,p_@querId int,@p_commandlist varchar(200)) asbeginendeg.if commands is having three values xx,yy,zz then three rows must be inserted into the table.i.e ResponseId queryId commands1 1 xx2 2 yy 3 3 zzCan anyone tell me how to do multiple inserts into the table.Thankssri |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-07-17 : 15:15:19
|
| What you need to do is PARSE a CSV (ie split a comma seperated list of values)Search this forum for "CSV" and "Parsing functions" for many topics on the subject.Be One with the OptimizerTG |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-18 : 05:33:33
|
| [code]CREATE FUNCTION ParseValues (@String varchar(8000),@Delimiter char(1) ) RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(1000) ) AS BEGIN DECLARE @Value varchar(100) WHILE @String is not null BEGIN SELECT @Value=CASE WHEN CHARINDEX(@Delimiter,@String) >0 THEN LEFT(@String,CHARINDEX(@Delimiter,@String)-1) ELSE @String END, @String=CASE WHEN CHARINDEX(@Delimiter,@String) >0 THEN SUBSTRING(@String,CHARINDEX(@Delimiter,@String)+1,LEN(@String)) ELSE NULL END INSERT INTO @RESULTS (Val) SELECT @Value END RETURN END Create procedure InsertCommandvalues(@p_ResponseId int,p_@querId int,@p_commandlist varchar(200)) asbegininsert into yourtableselect @p_ResponseId,@p_querId,ValFROM dbo.ParseValues(@p_commandlist,',')end[/code] |
 |
|
|
csri
Starting Member
45 Posts |
Posted - 2008-07-18 : 23:35:18
|
| Hi visakh16 Thank you very much for your response.Really appreciate your sample.My problem solved.Thankssri |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-19 : 01:25:35
|
quote: Originally posted by csri Hi visakh16 Thank you very much for your response.Really appreciate your sample.My problem solved.Thankssri
you're welcome |
 |
|
|
|
|
|
|
|