I'm working on a large SQL Server codebase, some of which has been in development since at least SQL 7 and possibly before.
Throughout the codebase, the method of raising an error is to use the following syntax which is, as far as I can tell, undocumented
RAISERROR <error number> <error message>
The error number
can be any value greater than 13000; no corresponding entry needs to exist in the sys.messages
table. The error message
can also be arbitrary.
The following sample code
raiserror 13000 'test error'
produces the following output
Msg 13000, Level 16, State 1, Line 1
test error
This behaviour is the same in SQL 2000, 2005 and 2008 (I haven't tested 2008 R2).
We're going to attempt to standardise on a supported method, but my question is where this behaviour came from in the first place.
I assume this must once have been documented, supported behaviour, but copies of books online for SQL 7 and before are difficult to find. Does anybody know when this was supported or when it was deprecated, if ever?
Edit
To clarify, according to the documentation, the supported RAISERROR
syntax is
RAISERROR ( { msg_id | msg_str | @local_variable }
{ ,severity ,state }
[ ,argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
And any user error number which is not defined in sys.messages
should be raised with an error message id of 50000
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.
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.
Is the user-defined severity level associated with this message. When using msg_id to raise a user-defined message created using sp_addmessage, the severity specified on RAISERROR overrides the severity specified in sp_addmessage . Severity levels from 0 through 18 can be specified by any user.
This looks like it might be a throwback to SQL Server's origins in Sybase, whose RAISERROR
command has this exact syntax:
Whatever its origins, this syntax is deprecated as of SQL Server 2008 R2 and will be removed in SQL Server 2012 (v 11.x). See the Deprecated Database Engine Features in SQL Server 2008 R2 page for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With