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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 loop through sp value and insert into table

Author  Topic 

kirank
Yak Posting Veteran

58 Posts

Posted - 2012-12-25 : 05:25:36
Hi,

one of my sp return records like this
@selectVal= 'a;b;c;d;f;e;r;g;'

i have one table call temp_res, on SP i need to write a logic where i can loop thr the string and insert into table(temp_res) just want it to split the result base on ;(semicolun) and insert new records

can you plz suggest me thanx in advance,

---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-25 : 08:57:32
[code];with CTE as
(
Select ID, LEFT(Data, CHARINDEX(';',Data+';')-1),
STUFF(Data, 1, CHARINDEX(';',Data+';'), '')
from temp_res
union all
select ID, LEFT(Data, CHARINDEX(';',Data+';')-1),
STUFF(Data, 1, CHARINDEX(';',Data+';'), '')
from CTE
where Data > ''
)

Select * from CTE
Order by ID[/code]
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2012-12-25 : 21:12:38
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-25 : 23:48:29
quote:
Originally posted by kirank

Hi,

one of my sp return records like this
@selectVal= 'a;b;c;d;f;e;r;g;'

i have one table call temp_res, on SP i need to write a logic where i can loop thr the string and insert into table(temp_res) just want it to split the result base on ;(semicolun) and insert new records

can you plz suggest me thanx in advance,

---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com



INSERT table(temp_res)
SELECT Val
FROM dbo.ParseValues(@selectVal,';')f


see ParseValues here

http://visakhm.blogspot.in/2010/02/parsing-delimited-string.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kirank
Yak Posting Veteran

58 Posts

Posted - 2012-12-26 : 05:18:50
@visakh16 , short and simple :)thanx


---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-27 : 00:03:52
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -