简体   繁体   English

在 SQL 查询中使用变量

[英]using variable in SQL query

I am using MySql database.我正在使用 MySql 数据库。 I have made following query and run at prompt, its running correctly.我已经进行了以下查询并在提示符下运行,它运行正常。

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

but when i use it in C# its giving error " Fatal error encountered during command execution "但是当我在 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");

Please guide.请指导。

MySqlCommand typically doesn't allow user variables in SQL statements by default.默认情况下, MySqlCommand通常不允许 SQL 语句中的用户变量。 To enable this, you have to add Allow User Variables = true;要启用此功能,您必须添加Allow User Variables = true; to your connection string.到您的连接字符串。 See https://mysqlconnector.net/connection-options/#AllowUserVariables for more details.有关更多详细信息,请参阅https://mysqlconnector.net/connection-options/#AllowUserVariables

It should work with MySql.Data, but you may have to switch from MySql.Data to MySqlConnector to fully get this working properly.它应该与 MySql.Data 一起使用,但您可能必须从 MySql.Data 切换到MySqlConnector才能完全正常工作。

you can't declare variable in raw query so, you should create a stored procedure, and call it in your code你不能在原始查询中声明变量,所以你应该创建一个存储过程,并在你的代码中调用它

You can do this?你可以这样做?

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

I have found an alternate solutions as;我找到了另一种解决方案:

Solution 1.解决方案 1。

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, 
    **sum(case when tr_type=1 then tr_qty else -(tr_qty) end) over (order by tr_date, srno rows unbounded preceding)+4 as running_total**
    FROM tblinvtransaction where tr_date between '2021-09-01' and '2021-09-09' and tr_itemcode = '02'
ORDER BY tr_date, tr_type

Solution 2.解决方案 2。

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
    ,**(
    SELECT SUM(case when tr_type=1 then tr_qty else -(tr_qty) end)
        FROM tblinvtransaction AS A
        WHERE A.srno <= B.srno and  A.tr_itemcode ='01'
    ) AS CumulativeSum**
FROM tblinvtransaction AS B
WHERE B.tr_itemcode ='01'
ORDER BY B.srno

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM