MYSQL Midstream, transaction, terminate stored Procedure method summary

Source: Internet
Author: User

Recently in the project development, there is a logic processing, need to run in the website, app, backstage, so that the maintenance of the late to bring a lot of inconvenience, easy to omit the app side or the background, so the Java code to convert to stored procedures, the logical processing written in the MySQL side, which encountered cursors, transaction processing. The problem is not difficult, just easy to forget, do a summary:

DECLARE err INT default 0; #声明一个整形变量err, default value is 0
DECLARE orderdone INT DEFAULT false;--traversal cursor End identity
DECLARE cur_order CURSOR for SELECT id,uid,productid,amount from P_order WHERE state=0 and Orderno=i_orderno LIMIT 0, 1; --Creation of cursors
DECLARE CONTINUE HANDLER for not FOUND SET orderdone=true; --Binding cursor end identification (not used in this program)
DECLARE CONTINUE HANDLER for SQLEXCEPTION set err=1, #当sqlexception HANDLER when snapping to an exception, setting err=1

Pro_begin:begin-In order to jump out of the preparation

Start transaction;--BEGIN TRANSACTION (Create transaction point)

--Take value from it
OPEN Cur_order; --Open cursor
FETCH Cur_order into Intid,intuid,intpid,intoamount; --Using Cursors
CLOSE Cur_order; --Close cursor

The above is a simple cursor usage because only the first value is taken, so there is no use of the loop value, the basic use flow of the cursor: Define the cursor, open, use, close

IF Num>0 Then
SET O_result = 3; --Mobile phone number already exists
LEAVE Pro_begin; --This is the step out of the stored procedure and not continue
END IF;

The following is the rollback and commit of a transaction, usually written at the end of the stored procedure.

IF Err = 1 Then
ROLLBACK;
ELSE
COMMIT;
END IF;

End

MYSQL Midstream, transaction, terminate stored Procedure method summary

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.