| Author |
Topic |
|
johnoxy
Starting Member
10 Posts |
Posted - 2007-05-02 : 00:43:07
|
| HiCould 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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-02 : 00:48:00
|
1 = 1 => TRUEWHILE ( TRUE ) KH |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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 |
 |
|
|
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 X002548Sure, it's just plain bad code
|
 |
|
|
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 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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 |
 |
|
|
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 ...endI change that to:while 1=1begin... get variables ...if @@rowcount = 0 break... work with variables ...endSaves one exists call.rockmoose |
 |
|
|
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 @datawhile @@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_StartLoop_End: CODO ERGO SUM |
 |
|
|
|