Is there a way to construct a Do While Loop in T-SQL. I didn't see any =
syntax in BOL for doing this. Essentially, I want to always run the the =
loop once, then check the loop expression to see if I can exit. In VB =
it would look something like this
Do
..
Loop While(something < 5)
Thanks,
--MichaelWhile (something < 5)
Begin
..do stuff
End
HTH
Thomas
"Raterus" <raterus@.hotmail.com> wrote in message
news:uf9oXJSRFHA.2932@.TK2MSFTNGP09.phx.gbl...
Is there a way to construct a Do While Loop in T-SQL. I didn't see any synt
ax
in BOL for doing this. Essentially, I want to always run the the loop once,
then check the loop expression to see if I can exit. In VB it would look
something like this
Do
..
Loop While(something < 5)
Thanks,
--Michael|||Let me be a bit more descriptive, "something" is set from within the =
loop. What I'm trying to avoid is having to set "Something" before the =
loop, and also in the loop, repeating the exact same syntax. A normal =
While loop won't work without duplicating the code from within the loop.
Do
Something =3D GetSomething()
DoSomething(Something)
Loop While (Something < 5)
"Thomas" <replyingroup@.anywhere.com> wrote in message =
news:OWtdcLSRFHA.2356@.TK2MSFTNGP14.phx.gbl...
> While (something < 5)
> Begin
> ...do stuff
> End
>=20
>=20
> HTH
>=20
>=20
> Thomas
>=20
>=20
> "Raterus" <raterus@.hotmail.com> wrote in message=20
> news:uf9oXJSRFHA.2932@.TK2MSFTNGP09.phx.gbl...
> Is there a way to construct a Do While Loop in T-SQL. I didn't see =
any syntax=20
> in BOL for doing this. Essentially, I want to always run the the loop =
once,=20
> then check the loop expression to see if I can exit. In VB it would =
look=20
> something like this
>=20
> Do
> ...
> Loop While(something < 5)
>=20
> Thanks,
> --Michael=20
>=20
>|||That's not the same as a do/while loop - with a do/while loop (as shown, or
in the C languages) the code in the loop runs at least once. A couple option
s:
1. Change your condition so it will always be true the first time; to
continue your example:
WHILE (something < 6) BEGIN ... END
2. Move the condition to an IF statement at the end of the loop:
WHILE (1=1)
BEGIN
-- code...
IF (something = 5) BREAK
END
3. Use a GOTO statement:
_LOOP:
-- ... CODE
IF (something < 5) GOTO _LOOP
"Thomas" wrote:
> While (something < 5)
> Begin
> ...do stuff
> End
>
> HTH
>
> Thomas
>
> "Raterus" <raterus@.hotmail.com> wrote in message
> news:uf9oXJSRFHA.2932@.TK2MSFTNGP09.phx.gbl...
> Is there a way to construct a Do While Loop in T-SQL. I didn't see any sy
ntax
> in BOL for doing this. Essentially, I want to always run the the loop onc
e,
> then check the loop expression to see if I can exit. In VB it would look
> something like this
> Do
> ...
> Loop While(something < 5)
> Thanks,
> --Michael
>
>|||That's an odd design pattern - declaring the variable for the exit condition
within the loop. If both GetSomething() and DoSomething(Something) are
deterministic then the condition will either always be false in which case
there's no reason to have it, or always be true in which case the loop will
never exit.
"Raterus" wrote:
> Let me be a bit more descriptive, "something" is set from within the loop. What I
'm trying to avoid is having to set "Something" before the loop, and also in the loo
p, repeating the exact same syntax. A normal While loop won't work without duplicat
ing
the code from within the loop.
> Do
> Something = GetSomething()
> DoSomething(Something)
> Loop While (Something < 5)
> "Thomas" <replyingroup@.anywhere.com> wrote in message news:OWtdcLSRFHA.235
6@.TK2MSFTNGP14.phx.gbl...
>|||Just an example off the top of my head, can't say it makes any sense to =
me either, the main thing I wanted was a loop that runs at least once.
"KH" <KH@.discussions.microsoft.com> wrote in message =
news:0DF4A1CF-94E9-4839-8624-DE769BCAE747@.microsoft.com...
> That's an odd design pattern - declaring the variable for the exit =
condition=20
> within the loop. If both GetSomething() and DoSomething(Something) are =
> deterministic then the condition will either always be false in which =
case=20
> there's no reason to have it, or always be true in which case the loop =
will=20
> never exit.=20
>=20
>=20
> "Raterus" wrote:
>=20
loop. What I'm trying to avoid is having to set "Something" before the =
loop, and also in the loop, repeating the exact same syntax. A normal =
While loop won't work without duplicating the code from within the loop.
news:OWtdcLSRFHA.2356@.TK2MSFTNGP14.phx.gbl...
any syntax=20
loop once,=20
would look=20|||Thanks, I had always "hacked" around it using methods like this. Just =
wanted to make sure I wasn't missing anything syntactically.
"KH" <KH@.discussions.microsoft.com> wrote in message =
news:0C77E8A0-3E6D-4C1D-A339-2040364DE56B@.microsoft.com...
> That's not the same as a do/while loop - with a do/while loop (as =
shown, or=20
> in the C languages) the code in the loop runs at least once. A couple =
options:
>=20
> 1. Change your condition so it will always be true the first time; to=20
> continue your example:
> WHILE (something < 6) BEGIN ... END
>=20
> 2. Move the condition to an IF statement at the end of the loop:
> WHILE (1=3D1)
> BEGIN
> -- code...
> IF (something =3D 5) BREAK
> END
>=20
> 3. Use a GOTO statement:
>=20
> _LOOP:
> -- ... CODE
> IF (something < 5) GOTO _LOOP
>=20
>=20
>=20
> "Thomas" wrote:
>=20
any syntax=20
loop once,=20
look=20|||Declare @.Done TinyInt Set @.Done = 0
While @.Done = 0 Begin
-- Do some work here
If GetSomething() = 'done' Set @.Done =1
End
"Raterus" wrote:
> Just an example off the top of my head, can't say it makes any sense to me
either, the main thing I wanted was a loop that runs at least once.
> "KH" <KH@.discussions.microsoft.com> wrote in message news:0DF4A1CF-94E9-48
39-8624-DE769BCAE747@.microsoft.com...
ing the code from within the loop.
>
No comments:
Post a Comment