【发布时间】:2021-11-16 18:32:03
【问题描述】:
我正在使用 MySql 数据库。我进行了以下查询并在提示符下运行,它运行正常。
SELECT
tr_date,
tr_invno,
particulars,
received,
issued,
@running_total:=@running_total + t.balance AS running_balance
FROM
(SELECT
srno,
tr_date,
tr_invno,
tr_type,
tr_accounthead as particulars,
case when tr_type=1 then tr_qty else 0 end received,
case when tr_type=2 then tr_qty else 0 end issued,
case when tr_type=1 then tr_qty else -(tr_qty) end balance
FROM tblinvtransaction where tr_date between '2021-09-01' and '2021-09-09' and tr_itemcode = '01') t
JOIN (SELECT @running_total:=(select sum(case when tr_type='1' then tr_qty else -(tr_qty) end) from tblinvtransaction where tr_date<'2021-09-01' and tr_itemcode = '01')
) r
ORDER BY t.tr_date, t.tr_type
但是当我在 C# 中使用它时,它给出错误“在命令执行期间遇到致命错误”
string query = @"SELECT tr_date, tr_invno, particulars, received, issued, @running_total:= @running_total + t.balance AS running_balance
FROM(SELECT srno, tr_date, tr_invno, tr_type, tr_accounthead as particulars,
case when tr_type = 1 then tr_qty else 0 end received,
case when tr_type = 2 then tr_qty else 0 end issued,
case when tr_type = 1 then tr_qty else -(tr_qty)end balance
FROM tblinvtransaction where tr_date between '2021-09-01' and '2021-09-09' and tr_itemcode = '01') t
JOIN(SELECT @running_total:= (select sum(case when tr_type = '1' then tr_qty else -(tr_qty)end) from tblinvtransaction where tr_date < '2021-09-01' and tr_itemcode = '01')
) r
ORDER BY t.tr_date, t.tr_type)";
string MySql = string.Concat(query);
MySqlDataAdapter da = new MySqlDataAdapter(MySql, connection);
DS_ItemLedger ds = new DS_ItemLedger();
da.Fill(ds, "DT_ItemLedger");
请指导。
【问题讨论】:
-
它可能有效,但官方不支持使用这样的变量。如果你的 MySQL 是现代的,比如 v8+,那么有适当的方法来计算总和。或者你可以在 c# 中做到这一点..
-
我正在使用v8+,请指导正确的运行总计方法。
-
什么是完整的异常消息和调用堆栈? idownvotedbecau.se/noexceptiondetails