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
 Old Forums
 CLOSED - General SQL Server
 for next loop

Author  Topic 

daviddeldave
Starting Member

28 Posts

Posted - 2003-03-10 : 11:57:01
In sql what is the equivalent for the for next loop in vb?

David Mercer

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-03-10 : 12:09:43
SQL is not a procedural language and thus contains no control-of-flow constructs.

Transact-SQL is a SQL Server specific extension of SQL. It does contain some control-of-flow language bits and pieces. You can read about that in Books Online by searching on "control-of-flow language".

Jay White
{0}
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-03-10 : 12:31:22
You can also use tables that can substitute values for the numbers in a loop. Search SQL Team for "tally" or "sequence" and you'll find a number of examples.

Go to Top of Page

rharmon
Starting Member

41 Posts

Posted - 2003-03-10 : 14:00:38
Begging your pardons, rob and page, but is this kinda what you are looking for?

declare @counter int
set @counter = 0
while @counter < 20
begin
set @counter = @counter + 1
print @counter
end

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-03-10 : 14:01:45
Wouldn't the following by sysnonomus?

FOR I=1 to 10
...some Code
NEXT I

SET I = 1
WHILE I < 11
BEGIN
...some Code
SET I = I + 1
END

What's the difference?

Brett

8-)

Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-03-10 : 14:21:16
quote:

Begging your pardons, rob and page, but is this kinda what you are looking for?


Nope. I'm not looking for anything.

quote:

SET I = 1
WHILE I < 11
BEGIN
...some Code
SET I = I + 1
END


I think T-SQL would prefer an @I ...

Jay White
{0}
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-03-10 : 14:29:28
My Bad Jay, Declare @I Int, it is.

But isn't that construct a control of flow, like a FOR/NEXT?

Brett

8-)



Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-03-10 : 14:33:08
Yeah, it is, but as Jay said it's not really SQL, it's an extension that T-SQL provides.

Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-03-10 : 14:46:28
... and daviddeldave did not ask for a t-sql extension ...

sql is to vb as apples is to oranges.

Microsoft spray paints their apples orange, which leads to much confusion ...

Jay White
{0}
Go to Top of Page

daviddeldave
Starting Member

28 Posts

Posted - 2003-03-12 : 14:39:30
Thanks the while loop is what I was looking for.

Regards

David Mercer
Go to Top of Page

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2003-03-12 : 14:52:19
quote:

sql is to vb as apples is to oranges.



Is that one of those SAT analogy questions?

Justin

Expect 0x80040106
Go to Top of Page
   

- Advertisement -