MySQL creates a stored procedure and traverses query results

Source: Internet
Author: User

Create procedure update_pointer () <br/> begin <br/>/* define variable 1 */<br/> declare total float; <br/> declare uid int; <br/> declare _ DONE int default 0; <br/> declare currentp int default 0; <br/> declare firstid int; <br/> declare secondid int; <br/> declare parentid varchar (16); <br/> declare first_jj_yy int default 0; # first-stage infrastructure fuel fee <br/> declare second_jj_ry int default 0; # basic fuel fee for the second Process <br/>/* define the cursor */<br/> declar E _ cur cursor for <br/> select alitotal, orduid, flightid, backfliid from fightorder where ordstate = 4; # Primary Order query <br/> declare _ currex cursor for <br/> select oeacttotal, ordid from flight_orderex where oestate = 4; </P> <p> declare continue handler for sqlstate '000000' SET _ DONE = 1; # error definition, mark the end of the loop </P> <p>/* Open the cursor */<br/> Open _ cur; <br/>/* cyclic execution */<br/> repeat <br/> fetch _ cur into total, uid, firstid, secondi D; <br/> if not _ done then <br/> If total> 0 and uid> 0 then <br/> select FCN + fyq into first_jj_yy from flightlist where flightid = firstid limit 1; <br/> If secondid> 0 then <br/> select FCN + fyq into second_jj_ry from flightlist where flightid = firstid limit 1; <br/> end if; <br/> Update sysusers set integral = (integral + total)-(second_jj_ry + first_jj_yy) Where sysusers. uid = uid; <br/> end if; <br/> Until _ done end repeat; # exit when _ DONE = 1 <br/>/* close the cursor */<br/> close _ cur; <br/> SET _ DONE = 0; # The New loop can continue only when it is defined as 0. <Br/> Open _ currex; <br/> repeat <br/> fetch _ currex into total, parentid; <br/> if not _ done then <br/> # If total> 0 then <br/> select orduid into uid from fightorder where fightorder. alipayno = parentid limit 1; # query user uid <br/> # insert into test values (total, UID); <br/> Update sysusers set integral = integral + total where sysusers. uid = uid; # Add Points <br/> # end if; <br/> until _ done end repeat; <br/> close _ currex; <br/> end 

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.