【问题标题】:using variable in SQL query在 SQL 查询中使用变量
【发布时间】: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

标签: c# mysql sql


【解决方案1】:

MySqlCommand 默认情况下通常不允许在 SQL 语句中使用用户变量。要启用此功能,您必须将Allow User Variables = true; 添加到您的连接字符串中。详情请见https://mysqlconnector.net/connection-options/#AllowUserVariables

它应该适用于 MySql.Data,但您可能必须从 MySql.Data 切换到 MySqlConnector 才能完全正常工作。

【讨论】:

    【解决方案2】:

    你不能在原始查询中声明变量 所以,你应该创建一个存储过程,并在你的代码中调用它

    【讨论】:

      【解决方案3】:

      你能做到吗?

      SELECT 
          tr_date,
          tr_invno,
          particulars,
          received,
          issued, 
          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,
          (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') as running_total
          FROM tblinvtransaction where tr_date between '2021-09-01' and '2021-09-09' and tr_itemcode = '01') t
      ORDER BY t.tr_date, t.tr_type
      

      【讨论】:

      • 出现未知列 running_total 错误。
      • 不做连接,调用 (select sum(case when tr_type='1' then tr_qty else -(tr_qty) end) from tblinvtransaction where tr_date
      • 现在错误已被删除,但运行总计计算出错。它没有添加上一行的总数。只需在所有行中添加第二个查询结果。
      【解决方案4】:

      我找到了替代解决方案:

      1。 选择 srno, tr_date, tr_invno, tr_type, tr_accounthead 作为详细信息, 当 tr_type=1 然后 tr_qty else 0 end 收到时, 如果 tr_type=2 然后 tr_qty else 0 end 发出, sum(当 tr_type=1 然后 tr_qty else -(tr_qty) end) over (order by tr_date, srno rows unbounded before)+4 as running_total FROM tblinvtransaction 其中 tr_date 在 '2021-09-01' 和 '2021-09-09' 之间且 tr_itemcode = '02' ORDER BY tr_date, tr_type

      2。 选择 srno, tr_date, tr_invno, tr_type, tr_accounthead 作为详细信息, 当 tr_type=1 然后 tr_qty else 0 end 收到时, 当 tr_type=2 然后 tr_qty else 0 end 发出的情况 ,( SELECT SUM(当 tr_type=1 然后 tr_qty else -(tr_qty) end 时的情况) FROM tblinvtransaction 作为 A WHERE A.srno FROM tblinvtransaction AS B WHERE B.tr_itemcode ='01' 由 B.srno 订购

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      相关资源
      最近更新 更多