Posted in Development

Difference between RAISERROR AND THROW in SQL Server

Hi friends, in SQL Server 2008 and earlier version we can define error handling using RAISERROR command. While later in SQL Server 2012 a new command was introduced THROW for error handling.

Syntax for RAISERROR is as follows:

RAISERROR ( { msg_id | msg_str | @local_variable }

{ ,severity ,state }

[ ,argument [ ,…n ] ] )

[ WITH option [ ,…n ] ]

Syntax of THROW is as follows:

THROW [ { error_number | @local_variable },

{ message | @local_variable },

{ state | @local_variable }

] [ ; ]

Newly introduced command THROW has many same components but there were also some differences like:

  1. RAISERROR used parenthesis to delimit parameters while THROW don’t.
  2. There is no severity parameter in THROW so severity is always set to 16. While in RAISERROR we can define severity.
  3. THROW always terminates the batch while RAISERROR don’t.

Let’s  issue some T-SQL statements for this:

–Batch will not terminate

RAISERROR (‘Hello World’,16,0)

PRINT ‘RAISERROR’;

GO

—Terminate the batch

THROW 50000, ‘Hello World ‘,0

Print ‘Throw error’

Raiserroe vs THROW

As you can from screenshot that second PRINT statement doesn’t work in case of THROW.

4. Statements before THROW must be terminated be semicolon (;).

5. You can’t issue THROW with a NOWAIT command in cause of buffer output.

Thats all friends for the day. Have a happy learning.

Advertisements

Author:

I am Kapil Singh Kumawat working on SQL Server since last 5 years. I am from Jaipur, Rajasthan, India and currently working in Cognizant Technology Solutions as SQL Server Developer. I have a good experience in writing queries, performance tuning, SSIS, SSAS, Power BI, Data migration and database designing. Apart from database I have interest in travelling,watching football and listening music. My blogs are also published on http://www.sqlservercentral.com/blogs/kapil-blogs/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s