MySQL stored procedure with a cursor

Source: Internet
Author: User
1 delimiter $
2
3 drop procedure if exists 'jf'. 'insert2' $
4
5 create procedure 'insert2' (in yhbh varchar (50), Out bjbh varchar (50 ))
6 begin
7 declare tempbjbh varchar (50) default 0;
8 declare done int default 0;
9 declare cur1 cursor for select ffgl. c_bjbh from ffgl, dj_info where ffgl. c_yhbh = 'u01010101 'and ffgl. n_fflx = dj_info.n_fflx order by ffgl. c_bjbh ASC;
10
11 declare continue handler for sqlstate '000000' set done = 1;
12 open cur1;
13 repeat
14 fetch cur1 into tempbjbh;
15 if not done then
16 set tempbjbh = Concat (tempbjbh ,"/");
17 set bjbh = Concat (bjbh, tempbjbh );
18 end if;
19 until done end repeat;
20 close cur1;
21 select bjbh;
22 end $
23
24 delimiter;

The cursor definition statement must be placed behind the definition of other variables. Otherwise, an error occurs. For example, if the positions of rows 8th and 9th are changed, an error is returned.

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.