Search code examples
sql-servercompatibility-mode

Why is a trailing SET inconsistently throwing an error in SQL?


I'm looking at an error from one of our web applications, and it was calling a stored procedure that was responsible for updating a record in the database.

This stored procedure has worked for weeks with no issues. Then one day it started throwing errors, while debugging we found the cause to be inside the stored procedure.

It basically had a statement like this

Begin
  // Do Stuff

  Set
End

So the SET never actually set anything. For some reason this runs in perfectly fine on our server, and was running fine on a client server until earlier today when it decided to start complaining. (Incorrect Syntax error)

Is there any type of SQL Server setting that would cause this sudden change in behaviour?

Clarification - The SET has always been in the procedures. And running a SET by itself, or as a sole statement in a stored procedure does in fact work for me. This is the problem, it shouldn't work. So is there anything that would cause it to work when it should be failing?


Solution

  • A procedure with a SET like that would normally fail to compile, even if the SET cannot be reached:

    alter procedure dbo.testproc as
        begin
        return 1;
        set
        end
    
    Incorrect syntax near the keyword 'SET'.
    

    Since the alter fails, I can't see how the procedure could end up in your database in the first place?

    Or maybe you were running in compatibility mode for SQL Server 2000 (which still allowed this.) Changing the compatibility mdoe to SQL Server 2005 or higher would then break the procedure.