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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 split and iterate

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2010-03-08 : 02:42:15
hi

I would like to pass one parameter into my stored procedure, this procedure could be a string of integers like this..

1;2;3;5;8

I then need to iterate each integer like this...

Select * from mytable where pid = @currentIntegerValue


Can someone show how this could be achived if possible?

karthik_padbanaban
Constraint Violating Yak Guru

263 Posts

Posted - 2010-03-08 : 02:53:21
did you mean this.

declare @a varchar(20)
set @a = '1;2;3;5;8'
set @a = replace(@a,';',',')
exec('select * from mytable where pid in ('+@a+')')

Karthik
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-08 : 02:55:36
Also search for Array+SQL Server in google/bing

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-08 : 10:17:40
also see

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

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

Go to Top of Page
   

- Advertisement -