Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Loop for several values
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01327001
Message ID:
01327006
Views:
12
I did it myself in the meantime (thanks to Sergey) with
DECLARE @Item VARCHAR(128), @pos INT, @STR varchar(max), @separator VARCHAR(16)
set @separator = ','
SET @STR = '873,904,1563,2188,2526'

	WHILE DATALENGTH(@STR) > 0
	BEGIN
		SET @pos = CHARINDEX(@separator, @STR)
		IF @pos = 0 
			SET @pos = DATALENGTH(@STR)+1
 
		SET @Item = LEFT(@STR, @pos -1 )
		SET @STR = SUBSTRING(@STR, @pos + DATALENGTH(@separator), 8000)
        Print 'Executing SP for ' + @Item
         did my code
END
but thanks a lot for the alternative solution.

>>Hi everybody,
>>
>>Suppose I have several PersonIDs I'd like to test my SP with. How can I create a loop to do it?
>>
>>Thanks in advance, though I do have an idea myself.
>
>
>DECLARE @Test TABLE (PersonId int /*or whatever*/, ID Int IDENTITY(1,1))
>INSERT INTO @Test (PersonId) VALUES (??)
>INSERT INTO @Test (PersonId) VALUES (??)
>INSERT INTO @Test (PersonId) VALUES (??)
>INSERT INTO @Test (PersonId) VALUES (??)
>INSERT INTO @Test (PersonId) VALUES (??)
>INSERT INTO @Test (PersonId) VALUES (??)
>
>DECLARE @PersonId      int -- or whatever
>DECLARE @id_min        int
>DECLARE @id_max        int
>SET @id_min = 1
>SELECT @id_max = MIN(Id) FROM @Test
>
>WHILE @id_min <= @id_max
>      BEGIN
>         SELECT @PersonId = PersonId FROM @Test WHERE Id = @id_min
>         --- Test your SP here
>         SET  @id_min =  @id_min + 1
>      END
>
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform