sqlsql-servert-sqlsql-scripts

T-SQL STOP or ABORT command in SQL Server


Is there a command in Microsoft SQL Server T-SQL to tell the script to stop processing? I have a script that I want to keep for archival purposes, but I don't want anyone to run it.


Solution

  • An alternate solution could be to alter the flow of execution of your script by using the GOTO statement...

    DECLARE  @RunScript bit;
    SET @RunScript = 0;
    
    IF @RunScript != 1
    BEGIN
    RAISERROR ('Raise Error does not stop processing, so we will call GOTO to skip over the script', 1, 1);
    GOTO Skipper -- This will skip over the script and go to Skipper
    END
    
    PRINT 'This is where your working script can go';
    PRINT 'This is where your working script can go';
    PRINT 'This is where your working script can go';
    PRINT 'This is where your working script can go';
    
    Skipper: -- Don't do nuttin!
    

    Warning! The above sample was derived from an example I got from Merrill Aldrich. Before you implement the GOTO statement blindly, I recommend you read his tutorial on Flow control in T-SQL Scripts.