Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否可以在MySQL UDF中的IF条件中声明游标_Php_Mysql_Sql Server_Database - Fatal编程技术网

Php 是否可以在MySQL UDF中的IF条件中声明游标

Php 是否可以在MySQL UDF中的IF条件中声明游标,php,mysql,sql-server,database,Php,Mysql,Sql Server,Database,我可以在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 S

我可以在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,但我发现,

游标声明必须出现在处理程序声明之前和之后 变量和条件声明

如果要在某些条件下使用该游标,则必须在if条件中使用FETCH cursor语法

下面是一些很好的教程,将详细介绍光标:

请使用它:

开始

结束

IF test = true THEN
    BEGIN 
        DECLARE ats_cursor CURSOR FOR SELECT distinct ats_id FROM ats_eb ats where  ats.year = year and  ats.edition_shortname = edition;
    END;
ELSE
    BEGIN 
        DECLARE ats_cursor CURSOR FOR SELECT distinct ats_id FROM ats_eb ats where  ats.year = year and  ats.edition_shortname = edition;
    END;
END IF;