Cursor traversal data table for MySQL stored procedure

Source: Internet
Author: User
Tags exception handling

Cursor traversal data table for MySQL stored procedure

Today write a MySQL stored procedures, according to their own needs to traverse a data table, because the stored procedures used not much, syntax is not very familiar with the stored procedures without debugging environment, took a lot of time to slowly fix, so leave a trace.

 1 BEGIN 2 DECLARE done INT DEFAULT 0; 3 4 DECLARE Currentlingqi INT; 5 6 DECLARE Shizuname VARCHAR (30); 7/* Declaration CURSOR */8 DECLARE RS cursor for SELECT NodeName, Lingqi from Socialrelation; 9/* Exception handling */10 DECLARE CONTINUE HANDLER for SQLSTATE ' 02000 ' SET done = 1;11 12/* Open cursor */13 opening  Rs     14 15//* Remove the value of the current record Lingqi field one by one, you need to make the maximum judgment */16 FETCH NEXT from RS to Shizuname, Currentlingqi;               17/* Traverse data table */18 REPEAT19 IF not do THEN20 SET Currentlingqi = Currentlingqi + 60;21 /* If the Reiki value is greater than the maximum allowable value after update, set to maximum */22 if Currentlingqi >= 1800 THEN23 update s                  Ocialrelation SET lingqi = 1800 WHERE NodeName = shizuname;24 ELSE25/* Otherwise, normal update */26 UPDATE socialrelation SET Lingqi = Lingqi + WHERE NodeName = shizuname;27 END if;28 EN D if;29 FETCH NEXT from RS toShizuname, currentlingqi;31 UNTIL done END repeat;33 34/* Close cursor */35 close rs;36 END 

  

Today write a MySQL stored procedures, according to their own needs to traverse a data table, because the stored procedures used not much, syntax is not very familiar with the stored procedures without debugging environment, took a lot of time to slowly fix, so leave a trace.

Cursor traversal data table for MySQL stored procedure

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.