错误代码:1324 Undefined CURSOR

时间:2016-02-01 05:22:46

标签: mysql

我刚刚开始学习MySQL,但在编写程序时遇到了麻烦。 它给了我一个未定义的游标错误。我在这个论坛上查了几个例子并更正了我的代码,但它仍然会引发同样的错误。

DELIMITER $  
CREATE PROCEDURE MY_PROC()  
BLOCK1: BEGIN  
DECLARE LOOP1_DONE BOOLEAN DEFAULT FALSE;  
DECLARE VAR_TKR VARCHAR(100) DEFAULT 0;  
DECLARE TKR_VALUE VARCHAR(100) DEFAULT 0;  
-- OTHER VARIABLE DECLARATIONS   
DECLARE CUR1 CURSOR FOR SELECT DISTINCT TKR FROM STG_TBL;  
DECLARE CONTINUE HANDLER FOR NOT FOUND SET LOOP1_DONE = TRUE;  
-- SET VARIABLE VALUES  
OPEN CUR1;  
CUR1_LOOP: LOOP  
FETCH CUR1 INTO VAR_TKR;  
IF LOOP1_DONE THEN  
CLOSE CUR1;  
LEAVE CUR1_LOOP;  
END IF;  
-- OTHER SELECT STATEMENTS AND CALCULATIONS   
BLOCK2: BEGIN  
DECLARE LOOP2_DONE BOOLEAN DEFAULT FALSE;  
DECLARE CUR1_DATA CURSOR FOR  
SELECT TKR_VALUE  
FROM STG_VALUE_TBL   
WHERE TKR = VAR_TKR  
ORDER BY TKR_DATE;  
DECLARE CONTINUE HANDLER FOR NOT FOUND SET LOOP2_DONE = TRUE;  
OPEN CUR1_DATA;  
-- SET VARIABLE VALUES  
CUR1_DATA_LOOP: LOOP  
FETCH CUR1_DATA INTO TKR_VALUE;  
IF LOOP2_DONE THEN  
CLOSE CUR1_DATA;  
LEAVE CUR1_DATA_LOOP;  
END IF;  
-- OTHER SELECT STATEMENTS AND CALCULATIONS   
END LOOP CUR1_DATA_LOOP;  
END BLOCK2;  
-- CLOSE CUR1_DATA;  <removed as not needed>
END LOOP CUR1_LOOP;  
-- CLOSE CUR1;  <removed as not needed>
END BLOCK1;  
$  
DELIMITER;

请在下表和示例数据中找到:

CREATE TABLE STG_TBL
(
ID      NUMERIC(38),
TKR     VARCHAR(10) NOT NULL
);

INSERT INTO STG_TBL VALUES (100000,'TKR1');
INSERT INTO STG_TBL VALUES (200000,'TKR2');
INSERT INTO STG_TBL VALUES (300000,'TKR3');

COMMIT;


CREATE TABLE STG_VALUE_TBL
(
TKR_DATE    DATE,
TKR         VARCHAR(10),
TKR_VALUE   DECIMAL(20,10),
ADDED_DATE  DATE
);

INSERT INTO STG_VALUE_TBL VALUES ('01-01-2015','TKR1','10.231','01-30-2016');
INSERT INTO STG_VALUE_TBL VALUES ('01-02-2015','TKR1','18.151','01-30-2016');
INSERT INTO STG_VALUE_TBL VALUES ('01-03-2015','TKR1','22.952','01-30-2016');

COMMIT;

0 个答案:

没有答案