我在mysql存储过程中遇到了游标提取循环的问题。我的存储过程运行一个重新排序过程,该过程可以正常工作,直到订单编号跳过一个数字的排序的最后一个记录。例如,如果我有10条记录并且排序顺序过程从1开始,则所有数字从1到10应该显示在结果记录中。但是,我的存储过程会跳过上一个案例中的最后一个计数10,并重新编号最终记录11,因此计数从9变为11.无论涉及的记录数是多少,都是这种情况。
程序的逻辑非常简单:
我有一个包含产品类型记录的表,其中包含一个sort_order列,用于根据常规批处理循环期间的使用情况对记录进行重新排序。
CREATE TABLE `PRODUCT_TYPE` (
`PRODUCT_TYPE_ID` int(11) NOT NULL AUTO_INCREMENT,
`PRODUCT_TYPE_NAME` varchar(45) NOT NULL,
`PRODUCT_CATEGORY_ID` int(11) DEFAULT NULL,
`LIFESPAN_MONTHS` int(11) DEFAULT NULL,
`USER_ID` int(11) DEFAULT NULL,
`UPDATED_BY` int(11) DEFAULT NULL,
`UPDATED_DATE` datetime DEFAULT NULL,
`CREATED_DATE` datetime DEFAULT NULL,
`CREATED_BY` int(11) DEFAULT NULL,
`REVIEWED` bit(1) NOT NULL DEFAULT b'0',
`SORT_ORDER` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`PRODUCT_TYPE_ID`),
KEY `fk_PRODUCT_TYPE_PRODUCT_CATEGORY1_idx` (`PRODUCT_CATEGORY_ID`),
KEY `fk_PRODUCT_TYPE_USERS1_idx` (`USER_ID`),
KEY `fk_PRODUCT_TYPE_USERS2_idx` (`UPDATED_BY`),
KEY `fk_PRODUCT_TYPE_USERS3_idx` (`CREATED_BY`),
CONSTRAINT `fk_PRODUCT_TYPE_PRODUCT_CATEGORY1` FOREIGN KEY (`PRODUCT_CATEGORY_ID`) REFERENCES `PRODUCT_CATEGORY` (`PRODUCT_CATEGORY_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_PRODUCT_TYPE_USERS1` FOREIGN KEY (`USER_ID`) REFERENCES `USERS` (`USER_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_PRODUCT_TYPE_USERS2` FOREIGN KEY (`UPDATED_BY`) REFERENCES `USERS` (`USER_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_PRODUCT_TYPE_USERS3` FOREIGN KEY (`CREATED_BY`) REFERENCES `USERS` (`USER_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=latin1;
我每晚运行以下存储过程,根据每种类型的引用次数重新排序产品类型记录,使用sort_order列记录订单。
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `REORDER_MANUFACTURERS`()
BEGIN
DECLARE DONE BOOL;
DECLARE MID INT;
DECLARE MNAME VARCHAR(255);
DECLARE USES INT;
DECLARE SORT_ORDER_COUNTER INT;
DECLARE CUR CURSOR FOR SELECT M.MANUFACTURER_ID, M.MANUFACTURER_NAME, COUNT(U.UNIT_ID) AS USES
FROM MANUFACTURERS M LEFT JOIN mydb.UNITS U ON M.MANUFACTURER_ID = U.MANUFACTURER_ID
GROUP BY M.MANUFACTURER_ID, M.MANUFACTURER_NAME
ORDER BY USES DESC, MANUFACTURER_NAME;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = TRUE;
SET SORT_ORDER_COUNTER = 0;
OPEN CUR;
READ_LOOP: LOOP
FETCH CUR INTO MID, MNAME, USES;
UPDATE MANUFACTURERS SET SORT_ORDER = SORT_ORDER_COUNTER WHERE MANUFACTURER_ID = MID;
IF DONE THEN
LEAVE READ_LOOP;
END IF;
SET SORT_ORDER_COUNTER = SORT_ORDER_COUNTER + 1;
END LOOP;
CLOSE CUR;
END
对于我的生活,我发现这个逻辑会导致计数跳过一个节拍的问题。任何帮助将不胜感激。
答案 0 :(得分:0)
更改:
FETCH CUR INTO MID, MNAME, USES;
UPDATE MANUFACTURERS SET SORT_ORDER = SORT_ORDER_COUNTER
WHERE MANUFACTURER_ID = MID;
IF DONE THEN
LEAVE READ_LOOP;
END IF;
致:
FETCH CUR INTO MID, MNAME, USES;
IF DONE THEN
LEAVE READ_LOOP;
END IF;
UPDATE MANUFACTURERS SET SORT_ORDER = SORT_ORDER_COUNTER
WHERE MANUFACTURER_ID = MID;
这是因为:
如果FETCH超过结果集中的最后一行,则目标字段或变量的值不确定,
NOTFOUND
属性返回TRUE
。
请参阅 :(这是在Oracle游标上,但也适用于其他游标):