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
 Looping

Author  Topic 

david_reinjal
Starting Member

36 Posts

Posted - 2007-06-15 : 07:59:54
Hi guys,

Can anybody tell me how to do looping in SQL?

Regards,
David

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-15 : 08:02:56
[code]WHILE <Condition>
Begin
Statement 1
...
Statement n
END[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-16 : 02:40:16
quote:
Originally posted by david_reinjal

Hi guys,

Can anybody tell me how to do looping in SQL?

Regards,
David

Can you tell me what you are trying to do in looping?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-06-16 : 10:01:57
quote:
Originally posted by harsh_athalye

WHILE <Condition>
Begin
Statement 1
...
Statement n
END


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"



better would be:

WHILE 1 < 2
Begin
print 'avoid loops in t-sql. better to use a set-based approach'
END



elsasoft.org
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-16 : 10:56:25
quote:
Originally posted by jezemine

quote:
Originally posted by harsh_athalye

WHILE <Condition>
Begin
Statement 1
...
Statement n
END


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"



better would be:

WHILE 1 < 2
Begin
print 'avoid loops in t-sql. better to use a set-based approach'
END



elsasoft.org


Well. That would end in endless loop

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-06-16 : 11:46:09
indeed. that's the point. that is, you keep reading the message until you get the point.


elsasoft.org
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2007-06-16 : 16:53:31
declare a variable as a counter and initialize it to 0 or your starting point. And increment it after every loop exit. as thus


declare @counter int
set @counter = 0
WHILE <Condition>
Begin
Statement 1
...
Statement n

set @counter = @counter + 1
END


As Madhivanan said, what are you trying to loop through ???

Go to Top of Page
   

- Advertisement -