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
 Outer loop help

Author  Topic 

Hommer
Aged Yak Warrior

808 Posts

Posted - 2014-07-14 : 11:01:38
Hey,

Somehow I just couldn't get this to work. I could only get it to loop through once. What did I miss?

declare @Row_No int =0;
declare @I int = 0;

while @Row_No < 3
begin
Set @Row_No +=1
while @I < 2
Begin
Set @I +=1
print @Row_No
print 'row then i'
print @I
END
END

Thanks!

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-07-14 : 11:34:13
Don't you need to reset the internal variable before each loop?
declare @Row_No int =0;
declare @I int = 0;

while @Row_No < 3
begin
Set @Row_No +=1
SET @I = 0;
while @I < 2
Begin
Set @I +=1
print @Row_No
print 'row then i'
print @I
END
END
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2014-07-14 : 11:40:57
Ah, thanks!

Without the reset, @I is maxed out at the end of first loop so it did not get me the second round.
Go to Top of Page
   

- Advertisement -