ST003 - Entire procedure body not enclosed within BEGIN...END rule is stupid

Consider this temporary stored procedure code:

CREATE OR ALTER PROCEDURE #ST003_EXAMPLE AS
BEGIN
  PRINT 'Great';
END;

-- some debug after END...
THROW 50000, 'Really!', 1;

GO -- Separated statement for easy executing whole window from SSMS

EXECUTE #ST003_EXAMPLE;


This example will of course throw error. Why? Because procedure (and trigger) body is whole statement (in this example separated by GO). NOT BEGIN..END subpart. This can lead to serious errors.


Answers

  • The point of the error is that your procedure in its entirety isn't encapsulated in a BEGIN/END block, not the entire statement. What it is looking for you to do is this;

    CREATE OR ALTER PROCEDURE #ST003_EXAMPLE AS
    BEGIN
    
    BEGIN
      PRINT 'Great';
    END;
    
    -- some debug after END...
    THROW 50000, 'Really!', 1;
    
    END;
    GO -- Separated statement for easy executing whole window from SSMS
    
    EXECUTE #ST003_EXAMPLE;
    
    
Sign In or Register to comment.