Study Notes (5)

Source: Internet
Author: User
Tags try catch

1. Transactions
Begin Tran start transaction, commit Tran commit transaction, rollback Tran roll back transaction, save Tran save transaction
The first three are very familiar. The last save Tran should be well understood and researched.
In fact, the name of the Save Tran save point is a bookmarkmark. For example, when many operations are performed in a transaction, a transaction is also divided into the first step, the second step, and the third step ..., if the first step is correct and the second step has an error, You need to roll back.
Add a save Tran firststep between them and roll back to the corresponding position through rollback Tran firststep.

2. Stored Procedure
When declaring the data type, you must also use the varying and output options when declaring the cursor type parameter.
Ooutput: output type parameter can be defined when output value is external
Return: all stored procedures actually return values. By default, a value of 0 is returned after the stored procedure is completed. to return a value, use return <integer return value>
If you want to return the stored procedure to prevent it from executing the following, you can directly use return.

3. handle errors
In SQL, you can also use try catch to handle errors. Of course, you can also use @ error to detect errors.

4. Manual generation Error
Raiserror (<message>, error level, status)
Example: raiserror ('error, please check the proc)

5. error level:
1-10: purely informative errors
11-16: If there is no error processing, the execution will be terminated and an error will be generated on the client. If there is an error processing block, the error processor will be called.
17: Only SQL server uses this level, which is very serious. For example, tampdb is full.
18-19: These two types of errors remind the system administrator
20-25: fatal error. User connection may be damaged
Note: The with log option is required for the second-class errors.

6. add your own error message
Add error message
Sp_addmessage [@ msgnum =] <Message ID> [@ severity =] <error level> [@ msgtext =] <'message string'> [, [@ lang =] <'linguistic '>] [, [@ with_log =] <true | false>] [, [@ replace =] 'repalce']
@ Replace: If you are editing an existing message instead of creating a new message, you must set this parameter to 'repalce'
Delete error messages
Sp_dropmessage

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.