Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

THROW vs. RAISERROR

I am trying to execute the following code:

THROW 51051, 'I come from the THROW construct :)', 1 ;

The error I am getting is:

Could not find stored procedure 'THROW'.

Isn't the THROW procedure a sytem procedure? Why can't it find it?

Furthhermore, what is the difference between unsing THROW and ErrorState ? Is one older/newer/better than the other? And what do "ErrorSeverity" and "ErrorState" mean by ErrorState? Can I define them as I will or they are predefined?

like image 271
Adam Avatar asked Jun 10 '14 13:06

Adam


People also ask

What is the difference between Raiserror and throw in SQL Server?

According to the Differences Between RAISERROR and THROW in Sql Server: Both RAISERROR and THROW statements are used to raise an error in Sql Server. The journey of RAISERROR started from Sql Server 7.0; whereas the journey of the THROW statement has just begun with Sql Server 2012.

What is Raiserror?

RAISERROR is a SQL Server error handling statement that generates an error message and initiates error processing. RAISERROR can either reference a user-defined message that is stored in the sys. messages catalog view or it can build a message dynamically.

Does Raiserror stop execution?

RaisError does not end processing of a batch. All you need to do is put a Return after the RaisError and the batch will stop there. Errors with a severity of 20 or higher stop the transaction and cause an immediate disconnect.

What is throw in SQL Server?

Throw is used to raises exception and transfers execution to a CATCH block in SQL server. Introduction. The THROW statement is introduced with SQL Server 2012. Throw is used to raise an exception and transfers execution to a CATCH block in SQL Server.


2 Answers

According to the Differences Between RAISERROR and THROW in Sql Server:

Both RAISERROR and THROW statements are used to raise an error in Sql Server.

The journey of RAISERROR started from Sql Server 7.0; whereas the journey of the THROW statement has just begun with Sql Server 2012.

Microsoft is suggesting we start using the THROW statement instead of RAISERROR. The THROW statement seems to be simpler and easier to use than RAISERROR.

like image 92
Eduardo Cuomo Avatar answered Oct 07 '22 06:10

Eduardo Cuomo


Yes, it is, but only since 2012. If you're using 2008R2, then it didn't exist.

The definitions of state and severity are clearly documented in the raiserror documentation

like image 41
podiluska Avatar answered Oct 07 '22 07:10

podiluska