• 28. 1. 2020
  • Ing. Jan Zedníček - Data Engineer & Controlling
  • 0

Some time ago, I published an article on the RAISERROR command, which raises an exception and allows us to influence whether the script will or will not continue based on severity. The THROW command is an alternative command introduced with SQL Server 2012.

THROW Syntax in SQL Server

The syntax is simpler compared to RAISERROR. Here’s the syntax for both commands for reference:

  • RAISERROR (msg, severity, state)
  • vs
  • BEGIN TRY
  • SELECT 1/0
  • END TRY
  • BEGIN CATCH
  • THROW [ { error_number | @local_variable },
  • { message | @local_variable },
  • { state | @local_variable } ]
  • [ ; ]
  • END CATCH

While RAISERROR accepts 3 arguments, THROW can be called (but not necessarily) within a TRY/CATCH error handling construct. If you try to execute the THROW command on its own, it will result in an error:

THROW excepltion

Outside a TRY/CATCH block, you can use the command like this:

THROW 51000, ‘The record does not exist.’, 1;

Rate this post

Ing. Jan Zedníček - Data Engineer & Controlling

My name is Jan Zedníček and I have been working as a freelancer for many companies for more than 10 years. I used to work as a financial controller, analyst and manager at many different companies in field of banking and manufacturing. When I am not at work, I like playing volleyball, chess, doing a workout in the gym.

🔥 If you found this article helpful, please share it or mention me on your website

Leave a Reply

Your email address will not be published. Required fields are marked *