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 2000 Forums
 SQL Server Development (2000)
 getting key in a comma delimited string

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-09-14 : 08:17:43
Phil writes "How do I get the first key, then the second, then the third etc. in a string of characters seperated by a comma? I want to loop through each key in the string and do some SQL statement's each time.

key1,key2,key3,key(whatever)

first time through the loop I want key1, then I want key2, etc.

I know how to get the first key, but how do I keep track of where I'm at in the loop?





Thanks,
Phil"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-09-14 : 08:20:53
Don't use loops. Try this:

http://www.sqlteam.com/item.asp?ItemID=2652

Once you have the results in a table, you can process the entire set in one operation, instead of working one-by-one in a loop.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2004-09-14 : 22:17:42
Have a look at this
http://www.nigelrivett.net/ParseCSVString.html
It returns a table from a csv string.

This one
http://www.nigelrivett.net/f_GetEntryDelimiitted.html
you pass the string and field number you want and it returns it
as in
select Forname = dbo.f_GetEntryDelimiitted (@s, 1, ',', 'Y') ,
Surname = dbo.f_GetEntryDelimiitted (@s, 2, ',', 'Y')


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -