我可以在if语句中声明游标吗?
如果可能的话我怎么能做到?
因为我只是制作了这样的光标
CREATE FUNCTION `fn_test`(
ProductID BIGINT(20)
)
RETURNS DECIMAL(10,2)
BEGIN
DECLARE PrductDiscValue DECIMAL(10, 2);
DECLARE DiscType INT(1);
DECLARE DiscValue DESIMAL(10,2);
IF ProductID != 0 THEN
SET PrductDiscValue = (SELECT Discountvalue, DiscountType FROM discount WHERE FIND_IN_SET(ProductID,DiscIDs);
END IF;
RETURN ProductDiscountValue(1);
但这不起作用.所以,我做了以下几点
IF ProductID != 0 THEN
DECLARE PrductDiscValue CURSOR FOR SELECT Discountvalue, DiscountType FROM discount WHERE FIND_IN_SET(ProductID,DiscIDs;
END IF;
OPEN ProductDiscountValue;
FETCH ProductDiscountValue INTO DiscValue, DiscType;
RETURN DiscValue;
END
这给了我错误::
1064 – 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第16行的’DECLARE ProdDiscValue CURSOR FOR SELECT Discountvalue,DiscountType FROM discount’附近使用正确的语法.
我需要DiscValue和DiscType来进行不同的计算.
任何帮助将不胜感激
提前致谢.
解决方法:
即使我到目前为止从未在MySQL中使用过CURSOR,但我发现它在MySQL manual中清楚地提到过
Cursor declarations must appear before handler declarations and after
variable and condition declarations.
如果要在某些条件下使用该游标,则必须在IF条件下使用FETCH CURSOR语法
下面是一些很好的教程,它将详细描述光标:
标签:php,mysql,database,sql-server
来源: https://codeday.me/bug/20190703/1369486.html