SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 IF within GOTO label
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SqlGirl87
Starting Member

26 Posts

Posted - 11/23/2012 :  17:49:23  Show Profile  Reply with Quote
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

United Kingdom
39 Posts

Posted - 11/23/2012 :  18:44:52  Show Profile  Reply with Quote
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 - 11/23/2012 :  19:38:00  Show Profile  Reply with Quote
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

United Kingdom
39 Posts

Posted - 11/23/2012 :  22:00:41  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000