Skip to content
Advertisement

Stored Procedure error – incorrect syntax near else

I’m working on my first stored procedure, and it’s showing an error at the else (where I have a comment at the else below). If anyone knows how I can fix the error, I’d appreciate it. I’ve tried looking online, and I think it may have to do with my begin/end and if/else positioning. I’m having trouble finding a similar example though.

Advertisement

Answer

The IF statement has nothing to do if it equates to true:

This is syntactically incorrect. TSQL is expecting a statement to be executed if the IF returns true, and in this case a comment is not sufficient.

If you don’t need to do anything if the statement is true, just switch to IF NOT EXISTS instead:

Otherwise if you want to use both the true and false outcomes of the IF statement:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement