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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 do while != while do

Author  Topic 

BorisCallens
Yak Posting Veteran

50 Posts

Posted - 2008-04-29 : 08:00:28
Hi,

Is it possible to have a
do [sql] while statement
instead of a
while [sql] do?
The difference being the first one has at least one run before the looping test is done and the second one is tested before a single query is done.

When searching, the second one is the one you always find.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-29 : 08:03:29
No. There is no DO in T-SQL.

Why don't you make the WHILE thingy part of an IF-statement if you need your logic this way?



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-29 : 08:08:12
Whats your full scenario?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-29 : 08:29:47
WHILE 1 = 1
BEGIN
... Code here
If Something IS True BREAK
END



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

BorisCallens
Yak Posting Veteran

50 Posts

Posted - 2008-04-29 : 08:34:03
Thx for the answers.

Yes, I'm implementing it with a break now.
But breaking in loops is considered bad programming practice, so I wanted to do avoid it. But it seems there's no way to do it differently.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-04-29 : 08:40:07
quote:
Originally posted by Peso

No. There is no DO in T-SQL.



All I could think of is Yoda when I saw that.

http://www.quotedb.com/quotes/42

I guess T-SQL and the ways of the Jedi do not mix.

For a Jedi, there is no TRY, only DO.
In T-SQL, there is no DO, but there is a TRY! (In SQL 2005+, anyway) ...


- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

BorisCallens
Yak Posting Veteran

50 Posts

Posted - 2008-04-29 : 09:14:23
Haha.
Ah.. ppl and their amazing powers to know a complete movie series by heart.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-04-29 : 18:47:42
There are plenty of ways to avoid using a break if you want to:
DECLARE @Continue BIT

-- Prime condition
IF EXISTS (SELECT * FROM Foo WHERE Bar = Yak)
BEGIN
SET @Continue = 1
END
ELSE
BEGIN
SET @Continue = 0
END

WHILE @Continue = 1
BEGIN
-- Do Stuff


-- Reset condition
IF EXISTS (SELECT * FROM Foo WHERE Bar = Yak)
BEGIN
SET @Continue = 1
END
ELSE
BEGIN
SET @Continue = 0
END
END
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-04-29 : 18:54:46
eh, just use break. so much cleaner.


elsasoft.org
Go to Top of Page
   

- Advertisement -