SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 loop through sp value and insert into table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kirank
Yak Posting Veteran

USA
52 Posts

Posted - 12/25/2012 :  05:25:36  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/25/2012 :  08:57:32  Show Profile  Reply with Quote
;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
Go to Top of Page

waterduck
Aged Yak Warrior

Malaysia
939 Posts

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

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 12/25/2012 :  23:48:29  Show Profile  Reply with Quote
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

USA
52 Posts

Posted - 12/26/2012 :  05:18:50  Show Profile  Reply with Quote
@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

India
52249 Posts

Posted - 12/27/2012 :  00:03:52  Show Profile  Reply with Quote
welcome

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000