Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RAISERROR issue since migration to SQL Server 2012

I am working on some issues with a website and database after a move of server. The database was previously SQL Server Express 2005 but is now running on SQL Server Express 2012.

The issues relate to the RAISERROR command and changes in 2012. I have looked at the documentation for the new syntax but am unsure on how I can pass both the error number and the message to the website.

Some examples of the RAISERROR commands in stored procedures and triggers are:

RAISERROR 50000 'Member with same Email address already exists.'

RAISERROR 44447 'The record can''t be added or changed. Referential integrity rules require a related record in table ''tblBrand''.'

RAISERROR 44446 'The record can''t be added or changed. Referential integrity rules require a related record in table ''tblFragranceHouse''.'

I have changed some to the new syntax but am unsure if I have done this correctly or not. I understand that if I just pass the error text it passes error number 50000. But I'm not sure what to do about the other error codes.

Any advice on how to translate these commands to 2012?

like image 442
johna Avatar asked Feb 10 '14 04:02

johna


People also ask

What command should you be using in new development instead of Raiserror?

New applications should use THROW instead of RAISERROR. Generates an error message and initiates error processing for the session. RAISERROR can either reference a user-defined message stored in the sys.

What is the difference between Raiserror and throw?

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 began with Sql Server 2012.

Does Raiserror stop execution SQL?

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 state in Raiserror in SQL Server?

Is an integer from 0 through 255. Negative values default to 1. Values larger than 255 should not be used. If the same user-defined error is raised at multiple locations, using a unique state number for each location can help find which section of code is raising the errors.


1 Answers

SQL 2012 does not support the undocumented version of Raiserror The supported syntax is

RAISERROR(@Message,Serverity,state); 

-- @Message could be message id , but it should exist in sysmessages, so if you want to send custom messages, I think you should add them sysmessages

http://msdn.microsoft.com/en-us/library/ms178592.aspx

Or the other option is to use THROW

http://technet.microsoft.com/en-us/library/ee677615.aspx

like image 144
Satheesh Variath Avatar answered Sep 20 '22 20:09

Satheesh Variath