如何将多个参数传递给mysql存储过程

时间:2015-05-07 20:30:22

标签: mysql stored-procedures

我正在处理的存储过程在尝试将参数传递给它时返回无效返回,与使用硬编码值运行时相比。

具有硬编码值的程序:

MyInterface var1<?> = new Imp1();

运行时,返回:16这是正确的。

具有两个输入参数的过程:

BEGIN
    SELECT COUNT(DISTINCT ItemID) 
    FROM ( 
        SELECT * 
        FROM sandbox_inventoryitempurchase 
        WHERE OrgID = '2781823' 
            AND PurchaseMonth>'2015-03-01'
    ) as DistinctCount;
END

输入参数定义为:

IN BEGIN SELECT COUNT(DISTINCT ItemID) FROM ( SELECT * FROM sandbox_inventoryitempurchase WHERE OrgID = orgid AND PurchaseMonth>sincedate ) as DistinctCount; END 整数,IN userid整数,IN orgid日期

运行时,返回:334,这是不正确的。

我是存储过程的新手,非常感谢我提供的任何帮助,说明我做错了什么以及我需要做些什么才能解决?

...谢谢

3 个答案:

答案 0 :(得分:0)

添加&#34; @&#34;在您的参数之前:

BEGIN 
    SELECT COUNT(DISTINCT ItemID) 
    FROM ( 
       SELECT * 
       FROM sandbox_inventoryitempurchase 
       WHERE OrgID = @orgid 
           AND PurchaseMonth>@sincedate
    ) as DistinctCount; 
END

快速的turtorial将是:Write-a-Stored-Procedure-in-SQL (请参阅本页末尾的参数使用方法......)

答案 1 :(得分:0)

尝试使用与列名不同的输入参数。 变化:

BEGIN
    SELECT COUNT(DISTINCT ItemID) 
    FROM ( 
        SELECT * 
        FROM sandbox_inventoryitempurchase 
        WHERE OrgID = orgid 
            AND PurchaseMonth>sincedate
    ) as DistinctCount;
END

要:

BEGIN
    SELECT COUNT(DISTINCT ItemID) 
    FROM ( 
        SELECT * 
        FROM sandbox_inventoryitempurchase 
        WHERE OrgID = p_orgid 
            AND PurchaseMonth > p_sincedate
    ) as DistinctCount;
END

传递IN p_orgid整数,IN p_sincedate date

解析器可能会看到WHERE OrgID = orgid,并且每个记录的评估结果为真,因为它评估为身份(与自身进行比较)。

答案 2 :(得分:-1)

参数名称和字段名称必须不同!

BEGIN SELECT COUNT(DISTINCT ItemID) FROM ( SELECT * FROM sandbox_inventoryitempurchase WHERE OrgID = p_orgid AND PurchaseMonth > p_sincedate ) as DistinctCount; END