SQL Server->> throw sentence vs. RAISERROR sentence

Source: Internet
Author: User
Tags throw exception

SQL Server 2012 begins by introducing a throw clause to replace the RAISERROR that has been used since SQL Server. Since the function is the same, it is all in try ... What is the difference between catch code blocks that do not catch errors and then throw errors?

RAISERROR statement THROW statement
If a msg_id is passed to RAISERROR, the ID must being defined in sys.messages. The Error_number parameter does not has the to is defined in sys.messages.
The MSG_STR parameter can contain printf formatting styles. The message parameter does not accept printf style formatting.
The severity parameter specifies the severity of the exception. There is no severity parameter. The exception severity is always set to 16.

The table above lists their differences

The biggest advantage of throw vs. RAISERROR is that we no longer need to capture the values of several system functions, such as error_message (), Error_state (), in the catch block, and then assign the values to the variables. Only a throw statement is required to complete the process where the original RAISERROR need to assign a variable and pass the argument to the throw exception. Also, the capture of the error line points directly to the error line, rather than the line of code that occurs when the RAISERROR statement takes place like RAISERROR. And throw can be as raiserror as can be passed, the effect and RAISERROR are the same.

The summary is that RAISERROR can do the throw can be done, and throw can also save RAISERROR the original need to complete a number of things. And Microsoft is also recommended to replace RAISERROR with throw.

-- Using THROW-1 BEGIN  SELECT1/0 as dividebyzeroEND  TRY  BEGIN  CATCH THROW; END CATCH GO

Results

(08134level1 6by zero error Encountered.

And if you use RAISERROR

 Use [Jerrydb]GO--Using RAISERROR ()DECLARE  @ERR_MSG  as NVARCHAR(4000) ,@ERR_SEV  as SMALLINT ,@ERR_STA  as SMALLINT BEGINTRYSELECT 1/0  asDivideByZeroENDTRYBEGINCATCHSELECT @ERR_MSG =error_message (),@ERR_SEV =error_severity (),@ERR_STA =error_state ()SET @ERR_MSG= 'Error occurred while retrieving the data from database:' + @ERR_MSG  RAISERROR(@ERR_MSG,@ERR_SEV,@ERR_STA) withNOWAITENDCATCHGO

Results

(050000level1   from Database  by zero error encountered.

Reference:

New THROW statement in SQL Server (vs RAISERROR)

SQL Server->> throw sentence vs. RAISERROR sentence

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.