How to capture SQL statement exceptions in MySQL

Source: Internet
Author: User

If you have friends who have used vs2005 development tools or other statements such as try catch block in JS, can you have sqlserver's @ error variable in MySQL, or, what about the try catch syntax in C?

The answer is yes, instanceCodeAs follows:

Drop procedure if exists sp_call_jobs;
Create procedure sp_call_jobs ()
Not deterministic
SQL Security definer
Comment''
Begin
Declare _ row, _ err, _ count int default 0;
Declare continue handler for sqlexception, sqlwarning, not found SET _ err = 1;
While _ row <3 do
Start transaction;
Insert into T1 (cond_val) values (null );
Commit;
If _ err = 1 then
Set _ COUNT = _ count + 1;
End if;
Set _ ROW = _ row + 1;
End while;
Select _ count;
End;

Statement:

Declare continue handler for sqlexception, sqlwarning, not found SET _ err = 1;

When an sqlexception, sqlwarning, and not found error occurs, SET _ err = 1 and execute the continue operation, that is, continue to execute the following statement.

This is similar to the try catch syntax in C.

We also use the transaction statement: Start transaction ;...... Commit; can ensure integrity

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.