Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've some doubts in GOTO Statement in SqlServer.
SQL
DECLARE	@a	Int
DECLARE	@b	Int
SELECT	@a	=	10
SELECT	@b	=	5
BEGIN
	IF		@a>@b GOTO	Greater
	IF		@a<@b GOTO	Lesser
END
Greater:	SELECT	@a
Lesser:		SELECT	@b


While executing these statements I get both labels executed. I suppose to get the value of "@a" only.


And below piece of code I got from MSDN.
But I don't understand why the "Branch_Two:" label doesn't get executed... I'm confused....


SQL
DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN 
    SELECT @Counter
    SET @Counter = @Counter + 1
    IF @Counter = 3 GOTO Branch_One  --Jumps to the first branch.
    IF @Counter = 4 GOTO Branch_Two  --This will never execute.
END
Branch_One:
    SELECT 'Jumping To Branch One.'
    GOTO Branch_Three; --This will prevent Branch_Two from executing.
Branch_Two:
    SELECT 'Jumping To Branch Two.'
Branch_Three:
    SELECT 'Jumping To Branch Three.'



Thanks in advance
Posted
Updated 22-Jan-18 17:51pm
v2

Read this it will give you some idea.
http://blog.sqlauthority.com/2007/05/07/sql-server-20052000-examples-and-explanation-for-goto/[^]

In the first code snippet the value of a is > b so GOTO Greater this is executed.
so Greater: SELECT @a this will get executed. after that next line is executed which is Lesser: SELECT @b .
since there is no statement to return after executing the code in label greater, the label lesser will get executed.

In the second code snippet when @Counter becomes 3 then
IF @Counter = 3 GOTO Branch_One is executed and the cursor comes out of while loop.
under label branch one these 2 statements are there

SELECT 'Jumping To Branch One.'<br />
    GOTO Branch_Three;<br />


in the above code after select, GOTO Branch_Three; statement is there so cursor will execute the statements under label branch_three
after finishing the execution of label branch_three
no statement is there to execute.
as the cursor came out of while loop branch_Two will never get executed.

I hope you understood.
 
Share this answer
 
v4
Comments
aswathy.s.88 14-Oct-11 1:56am    
Yes, I had visited that page already. But I could understand nothing from that article. That is why I asked help from Code Project Experts...
Check below

SQL
DECLARE @a  Int
DECLARE @b  Int
SELECT  @a  =   10
SELECT  @b  =   5
BEGIN
    IF      @a>@b GOTO  Greater
    ELSE IF     @a<@b GOTO  Lesser
END
Greater:    SELECT  @a  return
Lesser:     SELECT  @b return
 
Share this answer
 
v2
Comments
sachin10d 14-Oct-11 9:17am    
Changed elseif line and now it works fine
Try this

SQL
DECLARE	@a	Int
DECLARE	@b	Int
SELECT	@a	=	10
SELECT	@b	=	5
BEGIN
	IF		@a>@b GOTO	Greater
	IF		@a<@b GOTO	Lesser
END
Greater:	SELECT	@a
			GOTO Last;
Lesser:		SELECT	@b
			GOTO Last;
Last:


Here Last works as break; statment in C#
GOTO Last; is not added it will execute the Lesser part also

so you are getting both the outputs
above change works fine

Let me know if you have any drought

You can do in both the ways what kishorekke has done or as in the above script
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900