3

我对这个问题发疯了。

下面的存储过程打开一个游标,然后遍历结果以进行一些插入。

用于游标的 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;
4

1 回答 1

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
于 2012-06-11T09:32:30.097 回答