关于Mysql使用存储过程,并且使用游标的Demo

DELIMITER $$
/* Mysql 创建存储过程 名为:new_pro */
CREATE DEFINER=`root`@`localhost` PROCEDURE `new_pro`()
BEGIN
    /* 声明变量 并设定默认值 */
    Declare not_found int  default 0;
    Declare collectionId bigint;
    Declare children_collectionId bigint;
    /* 创建游标 通过查询表 channelcollection创建出游标 */
    Declare idCursor cursor for select id from channelcollection where name='a' or name='c';
    /* 异常处理,当发生异常的时候设定 not_found的值为1 */
    Declare continue handler for not found set not_found=1;
    /* 设定collectionId的值为 查询出的ID */
    set collectionId = (SELECT id FROM channelcollection where name='b' limit 1);
    /* 使用游标之前先打开游标 */
    open idCursor;
    /* 循环游标并且把循环出来的游标赋值给 children_collectionId */
    idCursor_loop: LOOP fetch idCursor into children_collectionId;
        if not_found=1 then
            leave idCursor_loop;
        else
            /* 将数据循环插入另外一个表 */
            insert into channelcollectionfamily values (collectionId,children_collectionId);
        end if;
    end LOOP idCursor_loop;
    /* 使用完之后关闭游标 */
    close idCursor;
END


转载于:https://my.oschina.net/Kxvz/blog/417352

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值