Sample mysql Stored Procedure Code

Source: Internet
Author: User

Drop procedure if exists P_SEQUENCE; /** temporarily omitting the package @ auto liangrui 2014/6/27 T_PRO_PRODUCT table sorting: sort the entire table by sequence number. Sort the sequence number by sequence number from the creation date of the repeat sequence number. In the test phase, set @ merid is called. = 'test66 '; call P_SEQUENCE (@ merid); **/create procedure P_PRODUCT_SEQUENCE (in v_merchar_id VARCHAR (100) begin DECLARE v_id VARCHAR (100); DECLARE v_rowNo VARCHAR (100); DECLARE flag int; DECLARE e_error integer default 0; -- defines the cursor declare c_cur CURSOR for Select. id, (@ rowNum: = @ rowNum + 1) as rowNo From T_PRO_PRODUCT a, (Select (@ rowNum: = 0) B where MERCHANT_ID = v_merchar_id order by ISNULL (. sequence),. sequence,. create_dt; declare continue handler for not found set flag = 1; declare continue handler for sqlexception set e_error = 1; SET flag = 0; OPEN c_cur; -- loop all rows repeat fetch c_cur INTO v_id, v_rowNo; update T_PRO_PRODUCT SET sequence = v_rowNo where ID = v_id; -- loop end until flag end repeat; -- CLOSE the cursor CLOSE c_cur; -- transaction processing IF e_error = 1 THENROLLBACK; ELSECOMMIT; end if; end



The above can be simplified.

SET @ colNo = 0;
UPDATE T_PRO_PRODUCT set sequence = (@ colNo: = @ colNo + 1) WHERE MERCHANT_ID = 'test66' order by sequence, CREATE_DT DESC;
 

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.