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.
| Author |
Topic |
|
sent_sara
Constraint Violating Yak Guru
377 Posts |
Posted - 2008-09-09 : 06:07:02
|
| Hi friends,can anyone tell the keyword to skip and continue the loop ,when the condition becomes trueWhen i=5 then printing should not be done for 5 and other flow should occurDeclare @i intset @i=10while(@i<=10) if @i=5 -->> keyword to insertprint @iSet @i=@i+1loop |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-09-09 : 06:31:06
|
I'm not sure I understand you but this is your code changed for proper tsql syntax....DECLARE @i INT SET @i = 10WHILE (@i <= 10) BEGIN IF @i = 5 BEGIN /* Put your stuff here */ PRINT '@i is 5!!!!' END ELSE BEGIN PRINT @i END SET @i = @i + 1END Of course because in your example above you are incrementing the counter (@i) and for some reason you are starting at 10 then the loop will only execute 1 time (and the condition (IF @i = 5) will never be true). Was that a typo from you and the @i variable was supposed to start at 0?-------------Charlie |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-09 : 08:03:38
|
| Can you explain what you are actually trying to do?MadhivananFailing to plan is Planning to fail |
 |
|
|
sent_sara
Constraint Violating Yak Guru
377 Posts |
Posted - 2008-09-10 : 02:51:26
|
hi i need to terminate the result of 5 (ie print 5) and continuing by printing 6,7,8,9the output should be1234678910quote: Originally posted by Transact Charlie I'm not sure I understand you but this is your code changed for proper tsql syntax....DECLARE @i INT SET @i = 10WHILE (@i <= 10) BEGIN IF @i = 5 BEGIN /* Put your stuff here */ PRINT '@i is 5!!!!' END ELSE BEGIN PRINT @i END SET @i = @i + 1END Of course because in your example above you are incrementing the counter (@i) and for some reason you are starting at 10 then the loop will only execute 1 time (and the condition (IF @i = 5) will never be true). Was that a typo from you and the @i variable was supposed to start at 0?-------------Charlie
|
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-09-10 : 04:06:14
|
| [code]Declare @i intset @i=1while(@i<=10) begin if @i<>5 print @i Set @i=@i+1end[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
sent_sara
Constraint Violating Yak Guru
377 Posts |
Posted - 2008-09-10 : 05:19:37
|
Txs madhivananquote: Originally posted by madhivanan
Declare @i intset @i=1while(@i<=10) begin if @i<>5 print @i Set @i=@i+1end MadhivananFailing to plan is Planning to fail
|
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-09-10 : 08:26:47
|
| you could use BREAK keyword as well to get out of the loop. |
 |
|
|
|
|
|