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 8942777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:36:15+00:00 2026-06-15T11:36:15+00:00

I have abstracted the problem to the following situation: I have a table (

  • 0

I have abstracted the problem to the following situation:

I have a table (A) that contains the number (Quantity) of items I want to update.
Next I have a table (SL) that contains the references to table (A) that I need to select from.
And finally the table that needs to get updated (B)

CREATE TABLE A
(
    Id int,
    Quantity int
)

CREATE TABLE SL
(
    Id int,
    A_Id int,
    S_Id int
)

CREATE TABLE B
(
    Id int,
    StatusValue int,
    A_Id int, 
    S_Id int NULL,
)

So let’s insert some data for testing purposes:

INSERT INTO A Values (1, 4), (2, 2), (3, 3), (4, 4), (5, 2)

delete from B
declare @i int = 1;
declare @j int = 0;
declare @maxA int = 5;
declare @rows_to_insert int = 10;
while @i < @maxA
begin
    while @j < @rows_to_insert * @i
    begin
        INSERT INTO B VALUES (10+@j, 0, @i, null)
        set @j = @j + 1
    end
    set @i = @i + 1
end
select * from B

INSERT INTO SL Values (1, 1, 1), (2, 2, 1), (3 ,2, 1)

And now on to the problem. I want to update TOP(Quantity) of records in B relating to the records in SL. Basically this is what I want to do, but it is unsupported in SQL:

DECLARE @Sale_Id int = 1;
WITH AB (AId, AQuantity, SaleId)
AS
(
    SELECT A.Id, A.Quantity, SL.S_Id FROM A
        INNER JOIN SL on A.Id = SL.A_Id
        WHERE SL.S_Id = @Sale_Id
)
UPDATE TOP(AB.Quantity) B
    SET StatusValue = 1,
        S_Id = AB.SaleId
    FROM AB
    WHERE StatusValue = 0 -- Edited
                AND B.A_Id = AB.AId

The error message is

Msg 4104, Level 16, State 1, Line 55
The multi-part identifier “AB.Quantity” could not be bound.

what are my options of getting this done?
(There is always the Cursor but is that a good option?)

Note: The data has a funny side to it that in SL there is 2 times a record referencing A_Id = 2. This implies that the result needs to have 4 B records with A_Id = 2 updated.

  • 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-15T11:36:16+00:00Added an answer on June 15, 2026 at 11:36 am

    So it seems the Cursor is the way I’ll need to go:

    DECLARE @A_Id int, @Quantity int;
    DECLARE ABCursor CURSOR LOCAL READ_ONLY FOR 
        SELECT A.Id, A.Quantity FROM A
            INNER JOIN SL on A.Id = SL.A_Id
            WHERE SL.S_Id = @Sale_Id
    OPEN ABCursor
    FETCH NEXT FROM ABCursor 
        INTO @A_Id, @Quantity
    WHILE @@FETCH_STATUS = 0
    BEGIN
        UPDATE TOP(@Quantity) B
            SET StatusValue = 1,
                S_Id = @Sale_Id
            WHERE StatusValue = 0
                AND B.A_Id = @A_Id
                AND S_Id is null
        FETCH NEXT FROM ABCursor 
            INTO @A_Id, @Quantity
    END 
    CLOSE ABCursor;
    DEALLOCATE ABCursor;
    

    Now must read up on what is the best definition for the Cursor i.e.: LOCAL READ_ONLY

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem. I have this pretty class and now I want
I have a number of classes and interface that are in the following structure
Consider the following problem: You have a class 'A' that serves as a base
I have the following problem in application architecture and am willing to solve it
I have the following problem: I have a lot of papers in pdf format
I am facing the following problem : I have a namespace Exception\* , which
i have the following problem. I have to parse a json request into an
I've just started using Fluent NHibernate and have run into the following problem trying
I have the following problem - made abstract to bring out the key issues.
I have the following problem: 1) There is some abstract class A with several

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.