我很担心这个问题。
下面的存储过程打开一个游标,然后迭代结果以进行一些插入。
用于游标的select语句在游标外部使用时(在sql窗口中)。但是它在光标中没有工作,光标在打开时返回NULL!
请注意,此代码位于存储过程中,它本身由另一个打开另一个游标的存储过程调用。不确定这个信息。很有用。
DECLARE location VARCHAR(255); -- location
DECLARE isLocationDone BOOL DEFAULT FALSE; -- status flag
DECLARE curLocation CURSOR FOR
SELECT `LOCATION` AS loc FROM `mfdtemp`.`opexstk_tmp`
WHERE `client` = in_client AND DATE(`rentdat`) < in_date
ORDER BY loc;
DECLARE
CONTINUE HANDLER FOR SQLSTATE '02000' SET isLocationDone = TRUE;
OPEN curLocation;
label_location_loop: LOOP
FETCH curLocation INTO location;
IF isLocationDone THEN
CLOSE curLocation;
LEAVE label_location_loop;
END IF;
INSERT INTO t01_stock_report_htry (
t01date, t01client, t01desc
)
VALUES (in_date, in_client, CONCAT_WS('', 'Stack ', location)
);
END LOOP;
答案 0 :(得分:1)
为什么不使用INSERT ... SELECT
?
INSERT INTO t01_stock_report_htry (t01date, t01client, t01desc)
SELECT in_date, in_client, CONCAT('Stack ', LOCATION)
FROM mfdtemp.opexstk_tmp
WHERE client = in_client AND DATE(rentdat) < in_date
ORDER BY LOCATION