简体   繁体   中英

Sytax error in mysql stored procedure

What is wrong in the syntax of the below stored procedure in 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 Message:

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

Changing the mysql query to the below also did not help and gave the following error: 删除@时出错

DECLARE用于本地(不带@ )或SET用于会话(带@

***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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM