mysql存储过程中的语法错误

时间:2014-03-23 15:16:13

标签: mysql

MySQL中以下存储过程的语法有什么问题?

CREATE PROCEDURE curdemo()
BEGIN
DECLARE @isbn varchar(17)
DECLARE @count int
DECLARE @price float(6,2)
DECLARE @totalbookpriceValue float(6,2)
DECLARE totalbookprice CURSOR
STATIC FOR 
(SELECT ISBN,COUNT(ISBN) FROM applieddb.cart c GROUP BY ISBN)
OPEN totalbookprice
IF @@CURSOR_ROWS > 0 BEGIN 
 FETCH NEXT FROM cur_emp INTO @isbn,@count
 WHILE @@Fetch_status = 0
SELECT ISBN,PRICE  FROM book INTO @price WHERE ISBN = @isbn 
PRINT 'Total Book Price is' + @price * @count
 FETCH NEXT FROM cur_emp INTO @isbn,@count
END
END
CLOSE totalbookprice
DEALLOCATE totalbookprice
SET NOCOUNT OFF
END

错误消息:

Error Code: 1064. 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 '@isbn varchar(17) DECLARE @count int DECLARE @price float(6,2) DECLARE @totalboo' at line 3

将mysql查询更改为下面也没有帮助,并给出以下错误: Error on getting rid of the @

2 个答案:

答案 0 :(得分:0)

DECLARE代表本地(没有@)或SET进行会话(@

答案 1 :(得分:0)

***The solution to the above problem is as below:***
CREATE DEFINER=`root`@`localhost` PROCEDURE `calculateTotalPrice`()
BEGIN
DECLARE done INT default 0;
DECLARE _isbn varchar(17);
DECLARE _count int;
DECLARE _price float(6,2);
DECLARE loop_cntr INT DEFAULT 0;
DECLARE _totalbookpriceValue float(6,2);
DECLARE totalbookprice CURSOR FOR  SELECT ISBN,COUNT(ISBN) FROM applieddb.cart c GROUP BY ISBN;
DECLARE CONTINUE HANDLER FOR NOT FOUND set done= TRUE;
OPEN totalbookprice;

the_loop: LOOP
FETCH totalbookprice into _isbn,_count;
SET _price= (SELECT b.price from book b where b.ISBN = _isbn);
SET _totalbookpriceValue= _price*_count;  
SELECT _totalbookpriceValue;
IF done THEN
        CLOSE totalbookprice;
        LEAVE the_loop;
END IF;
-- count the number of times looped
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
END