{"id":162,"date":"2019-09-24T07:59:58","date_gmt":"2019-09-24T07:59:58","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=162"},"modified":"2019-09-24T08:03:46","modified_gmt":"2019-09-24T08:03:46","slug":"raiseerror","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/raiseerror\/","title":{"rendered":"RaiseError"},"content":{"rendered":"\n<p>If you develop a new application, you should use the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-throw\/\">THROW<\/a><\/code>&nbsp;statement instead.<\/p>\n\n\n\n<h2>SQL Server&nbsp;<code>RAISEERROR<\/code>&nbsp;statement overview<\/h2>\n\n\n\n<p>The&nbsp;<code>RAISERROR<\/code>&nbsp;statement allows you to generate your own error messages and return these messages back to the application using the same format as a system error or warning message generated by SQL Server Database Engine. In addition, the&nbsp;<code>RAISERROR<\/code>&nbsp;statement allows you to set a specific message id, level of severity, and state for the error messages.<\/p>\n\n\n\n<p>The following illustrates the syntax of the&nbsp;<code>RAISERROR<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234<\/td><td>RAISERROR ( { message_id | message_text | @local_variable }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ ,severity ,state }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ ,argument [ ,&#8230;n ] ] )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ WITH option [ ,&#8230;n ] ];<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Let\u2019s examine the syntax of the&nbsp;<code>RAISERROR<\/code>&nbsp;for better understanding.<\/p>\n\n\n\n<h3>message_id<\/h3>\n\n\n\n<p>The&nbsp;<code>message_id<\/code>&nbsp;is a user-defined error message number stored in the&nbsp;<code>sys.messages<\/code>&nbsp;catalog view.<\/p>\n\n\n\n<p>To add a new user-defined error message number, you use the&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/\">stored procedure<\/a>&nbsp;<code>sp_addmessage<\/code>. A user-defined error message number should be greater than 50,000. By default, the&nbsp;<code>RAISERROR<\/code>&nbsp;statement uses the&nbsp;<code>message_id<\/code>&nbsp;50,000 for raising an error.<\/p>\n\n\n\n<p>The following statement adds a custom error message to the&nbsp;<code>sys.messages<\/code>&nbsp;view:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234<\/td><td>EXEC sp_addmessage &nbsp;&nbsp;&nbsp;&nbsp;@msgnum = 50005, &nbsp;&nbsp;&nbsp;&nbsp;@severity = 1, &nbsp;&nbsp;&nbsp;&nbsp;@msgtext = &#8216;A custom error message&#8217;;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>To verify the insert, you use the following query:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456<\/td><td>SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*FROM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.messagesWHERE &nbsp;&nbsp;&nbsp;&nbsp;message_id = 50005;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>To use this message_id, you execute the&nbsp;<code>RAISEERROR<\/code>&nbsp;statement as follows:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>RAISERROR ( 50005,1,1)<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Here is the output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>A custom error messageMsg 50005, Level 1, State 1<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>To remove a message from the&nbsp;<code>sys.messages<\/code>, you use the stored procedure&nbsp;<code>sp_dropmessage<\/code>. For example, the following statement deletes the message id 50005:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>EXEC sp_dropmessage &nbsp;&nbsp;&nbsp;&nbsp;@msgnum = 50005;&nbsp;&nbsp;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3>message_text<\/h3>\n\n\n\n<p>The&nbsp;<code>message_text<\/code>&nbsp;is a user-defined message with formatting like the&nbsp;<code>printf<\/code>&nbsp;function in C standard library. The&nbsp;<code>message_text<\/code>&nbsp;can be up to 2,047 characters, 3 last characters are reserved for ellipsis (\u2026). If the&nbsp;<code>message_text<\/code>&nbsp;contains 2048 or more, it will be truncated and is padded with an ellipsis.<\/p>\n\n\n\n<p>When you specify the&nbsp;<code>message_text<\/code>, the&nbsp;<code>RAISERROR<\/code>&nbsp;statement uses message_id 50000 to raise the error message.<\/p>\n\n\n\n<p>The following example uses the&nbsp;<code>RAISERROR<\/code>&nbsp;statement to raise an error with a message text:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>RAISERROR ( &#8216;Whoops, an error occurred.&#8217;,1,1)<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The output will look like this:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>Whoops, an error occurred.Msg 50000, Level 1, State 1<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3>severity<\/h3>\n\n\n\n<p>The severity level is an integer between 0 and 25, with each level representing the seriousness of the error.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123<\/td><td>0\u201310 Informational messages11\u201318 Errors19\u201325 Fatal errors<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3>state<\/h3>\n\n\n\n<p>The state is an integer from 0 through 255. If you raise the same user-defined error at multiple locations, you can use a unique state number for each location to make it easier to find which section of the code is causing the errors. For most implementations, you can use 1.<\/p>\n\n\n\n<h3>WITH option<\/h3>\n\n\n\n<p>The option can be&nbsp;<code>LOG<\/code>,&nbsp;<code>NOWAIT<\/code>, or&nbsp;<code>SETERROR<\/code>:<\/p>\n\n\n\n<ul><li><code>WITH LOG<\/code>&nbsp;logs the error in the error log and application log for the instance of the SQL Server Database Engine.<\/li><li><code>WITH NOWAIT<\/code>&nbsp;sends the error message to the client immediately.<\/li><li><code>WITH SETERROR<\/code>&nbsp;sets the&nbsp;<code>ERROR_NUMBER<\/code>&nbsp;and&nbsp;<code>@@ERROR<\/code>&nbsp;values to message_id or 50000, regardless of the severity level.<\/li><\/ul>\n\n\n\n<h2>SQL Server&nbsp;<code>RAISERROR<\/code>&nbsp;examples<\/h2>\n\n\n\n<p>Let\u2019s take some examples of using the&nbsp;<code>RAISERROR<\/code>&nbsp;statement to get a better understanding.<\/p>\n\n\n\n<h3>A) Using SQL Server&nbsp;<code>RAISERROR<\/code>&nbsp;with&nbsp;<code>TRY CATCH<\/code>&nbsp;block example<\/h3>\n\n\n\n<p>In this example, we use the&nbsp;<code>RAISERROR<\/code>&nbsp;inside a&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-try-catch\/\">TRY<\/a><\/code>&nbsp;block to cause execution to jump to the associated&nbsp;<code>CATCH<\/code>&nbsp;block. Inside the&nbsp;<code>CATCH<\/code>&nbsp;block, we use the&nbsp;<code>RAISERROR<\/code>&nbsp;to return the error information that invoked the&nbsp;<code>CATCH<\/code>&nbsp;block.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567891011121314151617<\/td><td>DECLARE &nbsp;&nbsp;&nbsp;&nbsp;@ErrorMessage&nbsp;&nbsp;NVARCHAR(4000), &nbsp;&nbsp;&nbsp;&nbsp;@ErrorSeverity INT, &nbsp;&nbsp;&nbsp;&nbsp;@ErrorState&nbsp;&nbsp;&nbsp;&nbsp;INT;&nbsp;BEGIN TRY&nbsp;&nbsp;&nbsp;&nbsp;RAISERROR(&#8216;Error occurred in the TRY block.&#8217;, 17, 1);END TRYBEGIN CATCH&nbsp;&nbsp;&nbsp;&nbsp;SELECT &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ErrorMessage = ERROR_MESSAGE(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ErrorSeverity = ERROR_SEVERITY(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@ErrorState = ERROR_STATE();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>&#8212; return the error inside the CATCH block<\/em>&nbsp;&nbsp;&nbsp;&nbsp;RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);END CATCH;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Here is the output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>Msg 50000, Level 17, State 1, Line 16Error occurred in the TRY block.<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3>B) Using SQL Server&nbsp;<code>RAISERROR<\/code>&nbsp;statement with a dynamic message text example<\/h3>\n\n\n\n<p>The following example shows how to use a local variable to provide the message text for a&nbsp;<code>RAISERROR<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456789<\/td><td>DECLARE @MessageText NVARCHAR(100);SET @MessageText = N&#8217;Cannot delete the sales order %s&#8217;;&nbsp;RAISERROR(&nbsp;&nbsp;&nbsp;&nbsp;@MessageText, <em>&#8212; Message text<\/em>&nbsp;&nbsp;&nbsp;&nbsp;16, <em>&#8212; severity<\/em>&nbsp;&nbsp;&nbsp;&nbsp;1, <em>&#8212; state<\/em>&nbsp;&nbsp;&nbsp;&nbsp;N&#8217;2001&#8242; <em>&#8212; first argument to the message text<\/em>);<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The output is as follows:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>Msg 50000, Level 16, State 1, Line 5Cannot delete the sales order 2001<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2>When to use&nbsp;<code>RAISERROR<\/code>&nbsp;statement<\/h2>\n\n\n\n<p>You use the&nbsp;<code>RAISERROR<\/code>&nbsp;statement in the following scenarios:<\/p>\n\n\n\n<ul><li>Troubleshoot Transact-SQL code.<\/li><li>Return messages that contain variable text.<\/li><li>Examine the values of data.<\/li><li>Cause the execution to jump from a&nbsp;<code>TRY<\/code>&nbsp;block to the associated&nbsp;<code>CATCH<\/code>&nbsp;block.<\/li><li>Return error information from the&nbsp;<code>CATCH<\/code>&nbsp;block to the callers, either calling batch or application.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you develop a new application, you should use the&nbsp;THROW&nbsp;statement instead. SQL Server&nbsp;RAISEERROR&nbsp;statement overview The&nbsp;RAISERROR&nbsp;statement allows you to generate your own error messages and return these messages back to the application using the same format&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,18,19],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/162"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":163,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/162\/revisions\/163"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}