MySQL存储过程的奇怪行为

时间:2015-06-18 09:07:44

标签: c# mysql stored-procedures

我在mysql中有一个存储过程,如下所定义

DROP PROCEDURE IF EXISTS subway.cust_inventory;
CREATE PROCEDURE subway.`cust_inventory`(IN lastItem INT,IN lastValue INT)
BEGIN
delete from temp_inventory;
drop table   temp_inventory;
create table  temp_inventory as
SELECT CONCAT ('390',
    cast(DATE (t6.InventoryDate) as char)
    ,cast(t6.SKUorItem as char)
    ) AS PK,t6.CustomerID, t6.entityID, t6.inventoryDate, t6.SKUorItem,      t6.Category, t6.inventory FROM
(
SELECT
'3' as CustomerID,
'90' as entityID,
t1.InventoryDate as inventoryDate, 
t1.idItem as SKUorItem,
t3.categoryInventary as Category,
@lastValue :=  if( @lastItem = t1.idItem, @lastValue + ifnull(t1.buyQty,0),    ifnull(t1.buyQty,0) ) as inventory,
@lastItem := t1.idItem
FROM
(
select InventoryDate, idItem, ifnull(sum(buyqty),0) as buyqty FROM(
(SELECT date(date) as InventoryDate, idItem, ifnull(Sum(movedQuantity),0) as buyqty
FROM subway.StockMovement
GROUP BY idItem, date(date)
)
UNION
(SELECT date(date) as InventoryDate, idItem,  ifnull(Sum(initialBuyQuantity),0) as buyqty
FROM subway.InvoiceStock where type = 'buy'  GROUP BY idItem, date(date))

UNION
 (select date(date) as inventorydate, idItem,ifnull(-sum(quantity),0) as  buyQty 
from subway.saleitem
group by idItem, date(date)
)
) t5
group by inventorydate,idItem
) t1
LEFT JOIN subway.item as t2 ON t1.iditem = t2.iditem
LEFT JOIN subway.CategoryInventary as t3 on t2.idCategoryInventary =    t3.idCategoryInventary
order by t1.iditem, t1.InventoryDate
) t6 where t6.inventorydate = '2015-06-09' order by inventoryDate,SKUorItem
;
END;

此过程在temp_inventory表中插入72条记录 当我从命令提示符调用过程或从forad调用mysql

时,这很有效

但是当我从下面指定的C#代码调用相同的程序时

mySQLConn.GetConnection();
            MySqlCommand cmd = new MySqlCommand("cust_inventory", mySQLConn.Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new MySqlParameter("lastItem", 0));
            cmd.Parameters.Add(new MySqlParameter("lastValue", 0));
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            MySqlDataReader myreader = new MySqlCommand("Select * from temp_inventory",mySQLConn.Conn).ExecuteReader();

这会在列清单(最后一列)中给出错误的值

知道为什么会发生这种情况

1 个答案:

答案 0 :(得分:3)

尝试以下代码。在作为参数值传递时,在MySql中以不同的方式处理0。

cmd.Parameters.Add(new MySqlParameter("lastItem",MySqlDbType.Int32).Value=0);
cmd.Parameters.Add(new MySqlParameter("lastValue", MySqlDbType.Int32).Value=0);

无需外部呼叫cmd.Dispose(); .Net Clr将在内部处理此事。

您只是将0值传递给这些参数,因此,只需在存储过程中创建两个变量并将值设置为0.无需在storedprocedure中创建任何参数,因此不需要从{{1}传递它们代码。

c#