Increment each row according to condition in SQL C#

Jul 9 2018 9:37 AM

I have a value of 100 from a textBox and I want to increment that value in the stock_out column but only till stock_out column is equal to stock_in column, if first row value is 40 than first row stock_out should be incremented by 10 from 100 than the remaining value from 100(90) should be incremented in the row below it and then row below it till needed (till 100 is 0). Items are ordered by Item_Name and Bill No asc.

 
Here is the code that I have been able to do so far, but it needs a lot of work and I do not have expertise in cte or any complicated c# coding. I am using SQL Server 2014 and c#.

C# code SQL Query

select * from stock_recieve  where Item_Name like'" + comboBoxitem.Text + "' where Stock_out < Stock_in  order by [Bill No] asc;  update stock_recieve       set Stock_out = Stock_out+" + qty + " where Stock_out < Stock_in;

Any help would be greatly appreciated.

P.S I know about the sql injection and I will change my sql queries to parameterized queries.


Answers (1)