T-sql STOP or ABORT command in SQL Server
To abort execution in T-SQL, use the RAISERROR
function with a severity of 20 or higher, which enforces a session disconnection:
Take note, this approach will immediately terminate the session and perform a rollback operation on the ongoing transaction.
Halting execution: Various approaches
Immediate exit with RAISEERROR
Putting a STOP to your script? RAISERROR
is your red card:
This technique stops execution and fires a message. Severity levels from 0 through 18 can be specified by any user.
Loop interruption with GOTO
For conditional halting, use the GOTO
statement. Make it take a leap!
This is your halt-at-will magic button to control your T-SQL's destiny.
Conditional execution using variables
Control freaks, rejoice! Use control variables and Rule the Algo World!
Script deactivation: The sleeper modes
The art of tactical sleeping: SET NOEXEC
A sleeper command for your all-nighter codes:
Here the script just rests a while, checks its syntax and then doesn't run unless you say so!
Disarm with IF condition
To disarm active scripts, the IF 0=1...
technique can be a lifesaver:
This is like the "play dead" tactic for your script.
Silent bye with RETURN
When you need the script to quietly exit, stage left:
This is your quiet goodbye, the Irish Exit of SQL.
Script error handling: RAISERROR in action
Spitting out custom messages with RAISERROR
Be dramatic! Throw custom alerts and warnings:
This nifty trick keeps your script execution status flashy and lively.
Usage fine prints: RAISERROR
When using RAISERROR
in severity level 20, be careful as the sysadmin privilege is a prerequisite. Consult MSDN documentation for understanding the error logging procedure precisely.
Was this article helpful?