我在整个网站上搜索了答案,但没有一个能解决我的问题,所以我要用一个新线程发布我的错误。
我的代码,
DELIMITER $$
USE `DB`$$
DROP PROCEDURE IF EXISTS `GET_Summary`$$
CREATE DEFINER=`connect`@`%` PROCEDURE `GET_Summary`(IN pDateFrom DATE,IN pDateTo DATE)
ROOT:BEGIN
DECLARE pTotal,pShortCode,pSignUp,pUnSub,pJunk INT;
DECLARE pCOM NVARCHAR(10);
DECLARE no_more_rows BOOLEAN;
CREATE TEMPORARY TABLE TMOMain
(GrandTotal INT,ShortCode INT,COM NVARCHAR(10),SignUp INT,UnSub INT, Junk INT);
DECLARE MoMainCur CURSOR FOR
SELECT COUNT(*),shortcode,(CASE WHEN ComID= 1 THEN 'A'
WHEN ComID= 2 THEN 'B' WHEN ComID= 3 THEN 'C' ELSE 'UV' END) AS COM
FROM tbl_inbox
INNER JOIN tbl_keyword ON keywordid = recvkeyword
WHERE recvDate >='2011-11-15' AND recvDate < '2011-11-16'
GROUP BY shortcode,COM;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN MoMainCur;
read_loop:LOOP
FETCH MoMainCur INTO pTotal,pShortCode,pCOM ;
INSERT INTO TMOMain
VALUES
(pTotal,pShortcode,pCOM ,0,0,0);
IF no_more_rows THEN
CLOSE MoMainCur;
LEAVE the_loop;
END LOOP;
-- CLOSE MoMain;
SELECT * FROM TMOMain;
END$$
DELIMITER ;
MySQL 5.1 版
错误信息:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE MoMainCur CURSOR FOR
最佳答案
来自fine manual :
DECLARE
is permitted only inside aBEGIN ... END
compound statement and must be at its start, before any other statements.Declarations must follow a certain order. Cursors must be declared before declaring handlers. Variables and conditions must be declared before declaring cursors or handlers.
强调我的。
将您的 CREATE TEMPORARY TABLE TMOMain
移动到所有DECLARE
之后。
关于mysql - 在存储过程中使用游标的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8192091/