Friday, June 27, 2014

SQL - Break execution of a SQL script

SET NOEXEC ON

SET NOEXEC ON skips the rest of the script but it does not terminate the connection.
To execute any other command you need to turn noexec off again.

OR

RAISERROR('Your error message', 20, -1) WITH log

This will terminate the connection, thereby stopping the rest of the script from running.
If you are logged in SQL as [admin] ('sysadmin' role), the RAISERROR will terminate the connection and stops the script from running the rest of code.
(If you are not logged in as [admin], otherwise the RAISEERROR() will fail and the script will continue executing.) Note that it requires both severity 20 (or higher) and "WITH LOG".

Share/Bookmark

No comments: