This is my table structure:
CREATE table Credit(id integer, organisationid int, availableCredit int)
INSERT INTO Credit VALUES (1, 1, 1000)
INSERT INTO Credit VALUES (2, 1, 100)
INSERT INTO Credit VALUES (3, 2, 600)
INSERT INTO Credit VALUES (4, 2, 400)
I have to reduce the available credit column value, I have amount 1050 with me. I need to reduce 1050 from credit table where organisation id = 1. Here the organisation Id 1 have 1100 available credit in total. The condition is the first inserted credit row should be updated first and then the rest (FIFO model), also the updation should happen only for organisationId = 1.
How do we update this using a single or multiple update statement?
Any suggestions?
Unfortunately, this is a rather messy thing to do in T-SQL – you’ll need something like a loop (cursor or
WHILEstatement).With this code here, I can get it to run – it’s not pretty, but it works.