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
 SQL looping with changing variable

Author  Topic 

gsm113
Starting Member

1 Post

Posted - 2012-11-08 : 19:01:38
Hi all,

I would like to run a set of SQL code on a repeat loop, with each loop using a different value for a specific parameter.

In other words, I have a data set (X,Y,Z) and I want to run the code passing X as a variable, using Y, and then again using Z.

If anyone can get me started down the right path I would greatly appreciate it.

chadmat
The Chadinator

1974 Posts

Posted - 2012-11-09 : 02:58:56
DECLARE @i INT

SET @i = 1

WHILE @i <=3
BEGIN

IF @i = 1 EXEC myProc 'X'
ELSE IF @i = 2 EXEC myproc 'Y'
ELSE IF @i = 3 EXEC myProc 'Z'

SET @i = @i + 1
END


-Chad
Go to Top of Page
   

- Advertisement -