Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8274445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:44:03+00:00 2026-06-08T07:44:03+00:00

This is my table structure: CREATE table Credit(id integer, organisationid int, availableCredit int) INSERT

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:44:05+00:00Added an answer on June 8, 2026 at 7:44 am

    Unfortunately, this is a rather messy thing to do in T-SQL – you’ll need something like a loop (cursor or WHILE statement).

    With this code here, I can get it to run – it’s not pretty, but it works.

    -- you want to reduce the total credits by this amount   
    DECLARE @AmountToReduce INT = 1050
    
    -- temporary variables    
    DECLARE @ID INT, @AvailableCredit INT
    
    -- get the first row from dbo.Credit that still has availableCredit - ordered by id
    SELECT TOP 1 @ID = id, @AvailableCredit = availableCredit
    FROM dbo.Credit
    WHERE availableCredit > 0 AND organisationId = 1
    ORDER BY id 
    
    -- as long as we still have credit to reduce - loop..   
    WHILE @AmountToReduce > 0 AND @ID IS NOT NULL
    BEGIN
        -- if we need to remove the complete availableCredit - do this UPDATE
        IF @AmountToReduce > @AvailableCredit
        BEGIN
            UPDATE dbo.Credit
            SET availableCredit = 0
            WHERE id = @ID
    
            SET @AmountToReduce = @AmountToReduce - @AvailableCredit
        END
        ELSE BEGIN
                -- if the amount to reduce left is less than the availableCredit - do this UPDATE
            UPDATE dbo.Credit
            SET availableCredit = availableCredit - @AmountToReduce
            WHERE id = @ID
    
            SET @AmountToReduce = 0
        END
    
        -- set @ID to NULL to be able to detect that there's no more rows left
        SET @ID = NULL
    
        -- select the next "first" row with availableCredit > 0 to process    
        SELECT TOP 1 @ID = id, @AvailableCredit = availableCredit
        FROM dbo.Credit
        WHERE availableCredit > 0 AND organisationId = 1
        ORDER BY id 
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
This is the table structure: CREATE TABLE `reports` ( `report_id` int(11) NOT NULL auto_increment,
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have this database structure, SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `announces` (
I have the following table structure: CREATE TABLE [Report].[MesReport]( [MesReportID] [int] IDENTITY(1,1) NOT NULL,
I'm using a table to save some text data with this structure: CREATE TABLE
I have the following table structure in my database: create table Encargado( ID int
Given the following table structure: CREATE TABLE foo ( ID INT NOT NULL AUTO_INCREMENT,
I have this table structure: CREATE TABLE users ( uid bigint NOT NULL, first_name
my table structure is CREATE TABLE [dbo].[Emp]( [ID] [int] NOT NULL, [EmpName] [varchar](50) NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.