mysql游标循环表与存储过程传参

电玩女神 2023-06-25 01:58 58阅读 0赞
  1. drop procedure if exists proc_tmp;
  2. create procedure proc_tmp(in cnt int)
  3. BEGIN
  4. /*这种写法也可以:DECLARE done INT DEFAULT FALSE;*/
  5. declare done int default 0; /*用于判断是否结束循环*/
  6. declare zoneCode varchar(100); /*用于存储结果集S_S的记录(因为我这里S_S的记录只有一列且为bigint类型)*/
  7. /*定义游标*/
  8. declare zoneCur cursor for select code from demo_zone where code!='Q07';
  9. /*定义 设置循环结束标识done值怎么改变 的逻辑*/
  10. declare continue handler for not FOUND set done = 1; /*done = true;亦可*/
  11. open zoneCur; /*打开游标*/
  12. /* 循环开始 */
  13. REPEAT
  14. /* 如果要fetch多列应该这样写,fetch idCur into rowId, rowName 但是注意rowId和rowName要先declare,且declare cur时也要select两列,且这两列和rowId、rowName对应 */
  15. fetch zoneCur into zoneCode; /*这部分可以看看书,还可以fetch多列(假设结果集S_S的记录不是单列的话)*/
  16. if not done THEN /*数值为非0,MySQL认为是true*/
  17. INSERT INTO `hznet_demo`.`sys_station`(`id`, `name`, `type`, `stationid`, `remark`, `longitude`, `latitude`, `enable_control`, `stationsts`, `stationpic`, `code`, `svnaddress`)
  18. 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`
  19. from sys_station a,(SELECT @rowNum:=(select count(1) from sys_station)) b,
  20. (select `name`, `type`, `remark`, `longitude`, `latitude`, `enable_control`, `stationsts`, `stationpic`, `code`, `svnaddress`
  21. from sys_station a, demo_station_config c
  22. where a.stationid=c.stationid and a.stationid not in (0,1,2,3)
  23. and c.zone_code=zoneCode limit 1) temp limit cnt;
  24. end if;
  25. until done end repeat;
  26. close zoneCur; /*关闭游标*/
  27. END
  28. /* 循环结束 */
  29. -- call proc_tmp(40);

发表评论

表情:
评论列表 (有 0 条评论,58人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mysql存储过程——游标

    1、游标的作用及属性 游标的作用就是用于对查询[数据库][Link 1]所返回的记录进行遍历,以便进行相应的操作;游标有下面这些属性: a、游标是只读的,也就是不能更新它;