mysql 存储过程 游标套用_mysql 存储过程 游标嵌套

基本表temp

包括 name, type, sendCity, getCity

分别对应物流送货司机名, 倒车的第几段, 发货城市, 收货城市

表结构

-- ----------------------------

-- Table structure for `temp`

-- ----------------------------

DROP TABLE IF EXISTS `temp`;

CREATE TABLE `temp` (

`id` int(16) NOT NULL AUTO_INCREMENT,

`name` varchar(32) DEFAULT NULL,

`type` varchar(32) DEFAULT NULL,

`sendCity` varchar(32) DEFAULT NULL,

`getCity` varchar(32) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of temp

-- ----------------------------

INSERT INTO `temp` VALUES ('7', '张三', '2', '北京', '沈阳');

INSERT INTO `temp` VALUES ('8', '张三', '3', '沈阳', '哈尔滨');

INSERT INTO `temp` VALUES ('9', '李四', '1', '保定', '天津');

INSERT INTO `temp` VALUES ('10', '李四', '2', '天津', '东莞');

INSERT INTO `temp` VALUES ('11', '李四', '3', '东莞', '广州');

INSERT INTO `temp` VALUES ('12', '王五', '1', '保定', '石家庄');

INSERT INTO `temp` VALUES ('13', '王五', '2', '石家庄', '宁晋');

这里是分段的,需要将姓名是“张三” 的第二,第三段合成一个 记录,也就是  “张三”   “北京” “哈尔滨”

物流司机可能是 1,2段,可能是1,2,3段,可能是2,3段所以在这里写一个存储过程,将合并后的结果放到另一个表中,

存储过程用到双游标嵌套,仅做记录

BEGIN

DECLARE done INT DEFAULT 0;

DECLARE _name varchar(64);

declare _maxType varchar(32);

declare _minType varchar(32);

declare _type varchar(64);

declare _sendCity varchar(64);

declare _getCity varchar(64);

#declare _tempName varchar(64);

declare _tempSend varchar(64);

declare _tempGet varchar(64);

DECLARE cursorOuter CURSOR for select name, max(type) maxType, min(type) minType from temp group by name;

DECLARE cursorInner CURSOR for select type, sendCity, getCity from temp where name=_name;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; #循环终止的标志,游标中如果没有数据就设置done为1

#select now();

OPEN cursorOuter;

FETCH cursorOuter INTO _name, _maxType, _minType;

WHILE (done=0) DO

begin

open cursorInner;

fetch cursorInner into _type, _sendCity, _getCity;

while (done=0) DO

if _type = _maxType then

set _tempGet = _getCity;

elseif _type = _minType then

set _tempSend = _sendCity;

end if;

FETCH cursorInner into _type, _sendCity, _getCity;

end while;

close cursorInner;

end;

INSERT into temp1(name, type, sendCity, getCity) values(_name, _type, _tempSend, _tempGet);

SET done = 0;

## 赋值下一个游标

FETCH cursorOuter INTO _name, _maxType, _minType;

END WHILE;

## 关闭

CLOSE cursorOuter;

END

最后的 temp1 表结构和数据是这样的:

-- ----------------------------

-- Table structure for `temp1`

-- ----------------------------

DROP TABLE IF EXISTS `temp1`;

CREATE TABLE `temp1` (

`id` int(16) NOT NULL AUTO_INCREMENT,

`name` varchar(32) DEFAULT NULL,

`type` varchar(32) DEFAULT NULL,

`sendCity` varchar(32) DEFAULT NULL,

`getCity` varchar(32) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of temp1

-- ----------------------------

INSERT INTO `temp1` VALUES ('19', '张三', '3', '北京', '哈尔滨');

INSERT INTO `temp1` VALUES ('20', '李四', '3', '保定', '广州');

INSERT INTO `temp1` VALUES ('21', '王五', '2', '保定', '宁晋');

遇到的问题:

1、存储过成总是提示 nodata,查询后 添加

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; #循环终止的标志,游标中如果没有数据就设置done为1

SET done = 0;

while循环以WHILE (done=0) DO  作为判断。

2、游标嵌套后需要加

begin……end;

内层循环是包含在其中的;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL存储过程可以使用游标来进行数据的遍历和操作。游标嵌套是指在一个游标循环内部再使用一个或多个游标循环,以实现更复杂的数据操作。 下面是一个简单的示例,演示了如何在MySQL存储过程嵌套使用游标: ``` DELIMITER // CREATE PROCEDURE nested_cursors() BEGIN DECLARE done INT DEFAULT FALSE; DECLARE outer_cursor CURSOR FOR SELECT id FROM table1; DECLARE inner_cursor CURSOR FOR SELECT value FROM table2 WHERE id = @current_id; OPEN outer_cursor; outer_loop: LOOP FETCH outer_cursor INTO @current_id; IF done THEN LEAVE outer_loop; END IF; OPEN inner_cursor; inner_loop: LOOP FETCH inner_cursor INTO @current_value; IF done THEN LEAVE inner_loop; END IF; -- 在这里进行数据操作,例如将数据插入到一个临时表中 END LOOP; CLOSE inner_cursor; END LOOP; CLOSE outer_cursor; END// DELIMITER ; ``` 在这个示例中,我们定义了两个游标:`outer_cursor` 和 `inner_cursor`。`outer_cursor` 用于遍历 `table1` 表中的所有记录,而 `inner_cursor` 则用于遍历 `table2` 表中与当前 `outer_cursor` 记录相关联的所有记录。 在存储过程的主循环中,我们首先打开 `outer_cursor`,然后进入一个无限循环,直到所有记录都被遍历完毕。在每次循环中,我们使用 `FETCH` 命令获取当前记录的 `id` 值,并将其存储在变量 `@current_id` 中。 接下来,我们打开 `inner_cursor`,并进入另一个无限循环,直到所有与当前 `@current_id` 相关联的记录都被遍历完毕。在每次循环中,我们使用 `FETCH` 命令获取当前记录的 `value` 值,并将其存储在变量 `@current_value` 中。 最后,在内部循环中,我们可以使用 `@current_id` 和 `@current_value` 变量进行数据操作,例如将数据插入到一个临时表中。完成内部循环后,我们关闭 `inner_cursor`,回到外部循环中,继续遍历下一个记录,直到所有记录都被遍历完毕。 注意,为了避免游标嵌套时出现问题,我们需要在每个游标的内部循环中使用唯一的变量名,否则可能会出现变量名冲突的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值