Muhammad Saleem
Muhammad Saleem

Reputation: 107

using variable in SQL query

I am using MySql database. 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"

        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.

Upvotes: 0

Views: 132

Answers (4)

Muhammad Saleem
Muhammad Saleem

Reputation: 107

I have found an alternate solutions as;

Solution 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.

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

Upvotes: 0

MestreDosMagros
MestreDosMagros

Reputation: 1028

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

Upvotes: 0

Bradley Grainger
Bradley Grainger

Reputation: 28207

MySqlCommand typically doesn't allow user variables in SQL statements by default. To enable this, you have to add Allow User Variables = true; to your connection string. See https://mysqlconnector.net/connection-options/#AllowUserVariables for more details.

It should work with MySql.Data, but you may have to switch from MySql.Data to MySqlConnector to fully get this working properly.

Upvotes: 1

pedram rankchian
pedram rankchian

Reputation: 39

you can't declare variable in raw query so, you should create a stored procedure, and call it in your code

Upvotes: 0

Related Questions