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
 While (1 = 1)

Author  Topic 

johnoxy
Starting Member

10 Posts

Posted - 2007-05-02 : 00:43:07
Hi
Could someone please explain the 1=1 part of 'While (1 = 1)'?

Cheers

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-05-02 : 00:44:53
It simply means Infinite loop.

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

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-05-02 : 00:48:00
1 = 1 => TRUE
WHILE ( TRUE )


KH

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-05-02 : 08:47:45
quote:
Originally posted by johnoxy

Hi
Could someone please explain the 1=1 part of 'While (1 = 1)'?

Cheers




Sure, it's just plain bad code



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-05-02 : 12:20:15
It could be used in a situation where you plan to exit a WHILE loop using the BREAK command:

while 1=1
begin
fetch next from Cur_Cursor into @data
if @@fetch_status <> 0 break
... do other stuff here ...
end


CODO ERGO SUM
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-05-02 : 12:24:00
quote:
Originally posted by Michael Valentine Jones

It could be used in a situation where you plan to exit a WHILE loop using the BREAK command:

while 1=1
begin
fetch next from Cur_Cursor into @data
if @@fetch_status <> 0 break
... do other stuff here ...
end




Again...

quote:
Originally posted by X002548


Sure, it's just plain bad code



Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-05-02 : 13:24:25
Brett, it's not bad code, unless you mean it's bad simply because it implies a loop and is therefore not set-based.

such constructs are often used in other languages where looping doesn't come with such a stigma.

my favorite is:

for (;;)
{
// do stuff forever, or until you hit a break
}



www.elsasoft.org
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-05-02 : 13:31:03
Are you kidding?

Why not just code the loop with the break logic?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-05-02 : 13:43:29
no, I'm not kidding.

sometimes there might be several different reasons why to break out of the loop, and you might want to break out in the middle of the loop, not at the beginning/end. in such a case the check to break out can't be in the while clause.


www.elsasoft.org
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2007-05-02 : 15:46:25
I find that often the break condition (in sql loops (yes I do have some, but no need to spread the word...))
are along the lines:

while not exists(yadayada)
begin
... get variables and work with them ...
end

I change that to:
while 1=1
begin
... get variables ...
if @@rowcount = 0 break
... work with variables ...
end

Saves one exists call.

rockmoose
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-05-02 : 20:39:05
For processing a cursor, I think an endless WHILE loop with a BREAK on a non-zero @@fetch_status makes more sense than coding the fetch statement twice. It is too easy to forget to change one FETCH statement and not the other when you make a code change.


while 1=1
begin
fetch next from Cur_Cursor into @data
if @@fetch_status <> 0 break
... do other stuff here ...
end

instead of:

fetch next from Cur_Cursor into @data

while @@fetch_status = 0
begin
... do other stuff here ...
fetch next from Cur_Cursor into @data
end


Of course, if you're really old school, you could do this...

Loop_Start:
fetch next from Cur_Cursor into @data
if @@fetch_status <> 0 goto Loop_End
... do other stuff here ...
goto Loop_Start
Loop_End:



CODO ERGO SUM
Go to Top of Page
   

- Advertisement -