November 3, 2005 at 2:28 pm
I have the if statement below in a stored proc. I can get each part to run fine individually, but if I try and wrap them in the conditional, I am getting the error:
Server: Msg 156, Level 15, State 1, Line 141
Incorrect syntax near the keyword 'else'.
How can I make this work?
Also, how do I end the IF so I can put the select statement outside instead of within the if and else. I tried an "end if" and I got the same error as above:
Server: Msg 156, Level 15, State 1, Line 172
Incorrect syntax near the keyword 'end'.
Thank you for your help!
IF @AndOr = 'Or'
INSERT INTO #AndOr blah blah blah
SELECT blah blah blah
FROM blah blah blah
WHERE blah blah blah
ORDER BY blah blah blah
INSERT INTO #AndOr blah blah blah
SELECT blah blah blah
FROM blah blah blah
WHERE blah blah blah
ORDER BY blah blah blah
SELECT * from #AndOr Order by blah blah blah
else
INSERT INTO #AndOr blah blah blah
SELECT blah blah blah
FROM blah blah blah
WHERE blah blah blah
ORDER BY blah blah blah
SELECT DISTINCT * from #AndOr Order by blah blah blah
November 3, 2005 at 2:47 pm
You will need to put a BEGIN....END statement. Put a BEGIN after the IF statment and and END before the ELSE Statement. This will execute all code between the BEGIN and END if the IF condition is TRUE.
Look up BOL for more info.
November 3, 2005 at 2:49 pm
Try:
IF @AndOr = 'Or'
BEGIN
INSERT INTO #AndOr blah blah blah
SELECT blah blah blah
FROM blah blah blah
WHERE blah blah blah
ORDER BY blah blah blah
INSERT INTO #AndOr blah blah blah
SELECT blah blah blah
FROM blah blah blah
WHERE blah blah blah
ORDER BY blah blah blah
SELECT * from #AndOr Order by blah blah blah
END
else
BEGIN
INSERT INTO #AndOr blah blah blah
SELECT blah blah blah
FROM blah blah blah
WHERE blah blah blah
ORDER BY blah blah blah
END
SELECT DISTINCT * from #AndOr Order by blah blah blah
November 3, 2005 at 2:53 pm
y'all rock! Thank you!
November 3, 2005 at 2:54 pm
If there is no BEGIN ... END only the first followin statement is included into IF.
After this 1st statement IF is considered finished and 2nd statement will be executed without taking in account the IF condition.
After that there is operator ELSE - and it's definetely wrong syntax.
_____________
Code for TallyGenerator
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply