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
 IF within GOTO label

Author  Topic 

SqlGirl87
Starting Member

26 Posts

Posted - 2012-11-23 : 17:49:23
I'm trying to use an IF in a GOTO label, but the problem I'm having is that SQL is ignoring the label and picking up the if statement regardless of it hitting the label or not.

Label1:
IF SOMETHING
SELECT SOMETHING
ELSE SELECT 'SOMETHING ELSE'

Even though the query itself isn't going to label1, sql is looking at the IF below at as a query of it's own, tried wrapping it in brackets but that didn't work.

Can this be done or will I have to use a CASE WHEN?

Thanks

Elizabeth B. Darcy
Starting Member

39 Posts

Posted - 2012-11-23 : 18:44:52
I am of the opinion that the problem you are facing is not in the code you have posted, but something above or below it which causes the control flow to change (the GOTO statement). Would you be able to post the remainder of the query?

I copied and ran the example on the page in the link shown below - and that indeed ran quite delightfully and as expected.
http://msdn.microsoft.com/en-us/library/ms180188.aspx

Now, I have a thing about the GOTO statement - simply put I am biased against it. But that shouldn't stop you from using it, of course.


________________________________________
-- Yes, I am indeed a fictional character.
Go to Top of Page

SqlGirl87
Starting Member

26 Posts

Posted - 2012-11-23 : 19:38:00
quote:
Originally posted by Elizabeth B. Darcy

I am of the opinion that the problem you are facing is not in the code you have posted, but something above or below it which causes the control flow to change (the GOTO statement). Would you be able to post the remainder of the query?

I copied and ran the example on the page in the link shown below - and that indeed ran quite delightfully and as expected.
http://msdn.microsoft.com/en-us/library/ms180188.aspx

Now, I have a thing about the GOTO statement - simply put I am biased against it. But that shouldn't stop you from using it, of course.


________________________________________
-- Yes, I am indeed a fictional character.





I see where it is going wrong now, my test isn;t using any GOTO the else clause is a simple select, so SQL isn't 'skipping' the label

If you run

DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN
SELECT @Counter
SET @Counter = @Counter + 1
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
IF @Counter = 5 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_Three:
SELECT 'Jumping To Branch Three.'
Branch_Two:
IF 1 = 2
SELECT '1 = 2'
ELSE
SELECT '1 <> 2'

It will still display '1 <> 2' - I assumed that a label would only be referenced if instructed by a GOTO guess not.
Go to Top of Page

Elizabeth B. Darcy
Starting Member

39 Posts

Posted - 2012-11-23 : 22:00:41
quote:
It will still display '1 <> 2' - I assumed that a label would only be referenced if instructed by a GOTO guess not.
Indeed!!

I think of SQL Server as a dumb thing, which just goes from one statement to the next, except when directed to do otherwise via a GOTO statement (or some other statements such as TRY-CATCH blocks).

So when it gets to the label "Branch_Three", it goes to the next statement which is a select, and from there down on past the "Brach_two:" label to the "if 1=2" statement and so on.


________________________________________
-- Yes, I am indeed a fictional character.
Go to Top of Page
   

- Advertisement -