【问题标题】:Weird Behaviour of MySQL stored procedureMySQL存储过程的奇怪行为
【发布时间】:2015-09-03 20:06:18
【问题描述】:

我在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 条记录 当我从命令提示符或从 toad for 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();

这在列库存(最后一列)中给出了错误的值

知道为什么会这样

【问题讨论】:

  • 只有两个问题:为什么不让存储过程返回结果,而不是从 C# 应用程序发出 2 个调用?如果从应用程序的另一个实例调用存储过程,而第一个实例尝试从 temp_inventory 读取,会发生什么情况?
  • 我使用的方法与您建议的方法相同,但经过多次尝试,它在命令行上运行良好,但在 c# 中却不行,那就是我引入了临时表,将撤消并切换回来
  • 好吧,我会尝试解决这个问题,而不是您当前的方法 - 这肯定会造成麻烦。
  • 代替参数并声明变量并将其初始化为0,SET @lastValue=0,@lastItem=0;工作,不知道有什么区别`DECLARE lastValue int default 0;并设置@lastValue=0;在 c# 中只有后者有效,在命令行中都有效

标签: c# mysql stored-procedures


【解决方案1】:

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

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。无需在存储过程中创建任何参数,因此无需从 c# 代码中传递它们.

SET @lastValue=0, @lastItem=0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2014-12-13
    • 2017-06-21
    • 1970-01-01
    • 2021-06-10
    相关资源
    最近更新 更多