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
 skip the process

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 true
When i=5 then printing should not be done for 5 and other flow should occur

Declare @i int
set @i=10

while(@i<=10)

if @i=5 -->> keyword to insert

print @i

Set @i=@i+1
loop


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 = 10

WHILE (@i <= 10) BEGIN

IF @i = 5 BEGIN
/*
Put your stuff here
*/
PRINT '@i is 5!!!!'
END
ELSE BEGIN
PRINT @i
END

SET @i = @i + 1
END


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
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-09 : 08:03:38
Can you explain what you are actually trying to do?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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,9
the output should be
1
2
3
4
6
7
8
9
10
quote:
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 = 10

WHILE (@i <= 10) BEGIN

IF @i = 5 BEGIN
/*
Put your stuff here
*/
PRINT '@i is 5!!!!'
END
ELSE BEGIN
PRINT @i
END

SET @i = @i + 1
END


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

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-10 : 04:06:14
[code]Declare @i int
set @i=1
while(@i<=10)
begin
if @i<>5
print @i
Set @i=@i+1
end
[/code]


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-09-10 : 05:19:37
Txs madhivanan

quote:
Originally posted by madhivanan

Declare @i int
set @i=1
while(@i<=10)
begin
if @i<>5
print @i
Set @i=@i+1
end



Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -