mysql游标循环表与存储过程传参
发布日期:2021-06-29 11:46:47 浏览次数:2 分类:技术文章

本文共 1548 字,大约阅读时间需要 5 分钟。

drop procedure if exists proc_tmp;create procedure proc_tmp(in cnt int)BEGIN/*这种写法也可以:DECLARE done INT DEFAULT FALSE;*/declare done int default 0;  /*用于判断是否结束循环*/declare zoneCode varchar(100); /*用于存储结果集S_S的记录(因为我这里S_S的记录只有一列且为bigint类型)*//*定义游标*/declare zoneCur cursor for select code from demo_zone where code!='Q07';/*定义 设置循环结束标识done值怎么改变 的逻辑*/declare continue handler for not FOUND set done = 1; /*done = true;亦可*/open zoneCur;  /*打开游标*//* 循环开始 */REPEAT/* 如果要fetch多列应该这样写,fetch idCur into rowId, rowName 但是注意rowId和rowName要先declare,且declare cur时也要select两列,且这两列和rowId、rowName对应 */fetch zoneCur into zoneCode;  /*这部分可以看看书,还可以fetch多列(假设结果集S_S的记录不是单列的话)*/if not done THEN  /*数值为非0,MySQL认为是true*/INSERT INTO `hznet_demo`.`sys_station`(`id`, `name`, `type`, `stationid`, `remark`, `longitude`, `latitude`, `enable_control`, `stationsts`, `stationpic`, `code`, `svnaddress`) select 10000000+@rowNum:=@rowNum + 1, temp.`name`, temp.`type`, 10000000+@rowNum:=@rowNum, temp.`remark`, temp.`longitude`+0.1, temp.`latitude`+0.1, temp.`enable_control`, temp.`stationsts`, temp.`stationpic`, temp.`code`, temp.`svnaddress`from sys_station a,(SELECT @rowNum:=(select count(1) from sys_station)) b,(select `name`, `type`, `remark`, `longitude`, `latitude`, `enable_control`, `stationsts`, `stationpic`, `code`, `svnaddress`from sys_station a, demo_station_config cwhere a.stationid=c.stationid and a.stationid not in (0,1,2,3) and c.zone_code=zoneCode limit 1) temp limit cnt;end if;until done end repeat;close zoneCur;  /*关闭游标*/END/* 循环结束 */-- call proc_tmp(40);

 

转载地址:https://blog.csdn.net/zzchances/article/details/103678814 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:nginx配置文件里user只能是root,否则报403错误
下一篇:mysql处理跨连接(dblink)

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月04日 08时55分12秒