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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:46:00+00:00 2026-06-08T03:46:00+00:00

I’m in SQL 2005 and I’m trying to convert this Cursor into something that

  • 0

I’m in SQL 2005 and I’m trying to convert this Cursor into something that isn’t a Cursor to determine if this is the most efficient way to do this.

        --Create cursor to determint total cost
DECLARE CostCursor CURSOR FAST_FORWARD
        FOR SELECT  ReceiptQty
                   ,Price
            FROM    @temp_calculate
            ORDER BY UpdateDate DESC
OPEN CostCursor 
FETCH Next FROM CostCursor INTO @ReceiptQty,@Price
WHILE @@FETCH_STATUS = 0
      BEGIN
            IF @OnHandQty >= @ReceiptQty
               BEGIN
                           --SELECT @ReceiptQty,@Price, 1,@OnHandQty
                     SET @Cost = @ReceiptQty * @Price
                     SET @OnHandQty = @OnHandQty - @ReceiptQty
                     SET @TotalCost = @TotalCost + @Cost
               END
            ELSE
               BEGIN
                     IF @OnHandQty < @ReceiptQty
                        BEGIN
                              --SELECT @ReceiptQty,@Price, 2,@OnHandQty
                              SET @Cost = @OnHandQty * @Price
                              SET @OnHandQty = 0
                              SET @TotalCost = @TotalCost + @Cost
                              BREAK;
                        END
               END
            FETCH Next FROM CostCursor INTO @ReceiptQty,@Price
      END
CLOSE CostCursor
DEALLOCATE CostCursor

The system needs to go through and use the newest recieved inventory and price to determine what the paid for the on-hand is.

Ex. 1st Iteration: @OnHandQty = 8 RecievedQty = 5 Price = 1 UpdateDate = 1/20 Results:  @HandQty = 3 @TotalCost = $5
2nd Iteration: @OnHandQty = 3 RecievedQty = 6 Price = 2 UpdateDate = 1/10 Results:  @HandQty = 0 @TotalCost = $11

The Final Results tell me that the inventory I have on hand I paid $11 for. If I was doing this in C# or any other Object Oriented langauge this screams Recursion to me. I thought about a Recursive CTE could be more efficient. I’ve only successfully done any Recursive CTE’s for Heirarchy following types of Queries and I haven’t been able to successfully wrap my head around a query that would achieve this another way.

Any help or a simple thats how it has to be would be appreciated.

  • 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-08T03:46:03+00:00Added an answer on June 8, 2026 at 3:46 am
    CREATE CLUSTERED INDEX IDX_C_RawData_ProductID_UpdateDate ON #RawData (ProductID ASC , UpdateDate DESC , RowNumber ASC)
    
        DECLARE @TotalCost Decimal(30,5)
        DECLARE @OnHandQty Decimal(18,5)
        DECLARE @PreviousProductID Int
    
        UPDATE  #RawData
        SET     @TotalCost = TotalCost = CASE
                                              WHEN RowNumber > 1
                                              AND @OnHandQty >= ReceiptQuantity THEN @TotalCost + (ReceiptQuantity * Price)
                                              WHEN RowNumber > 1
                                              AND @OnHandQty < ReceiptQuantity THEN @TotalCost + (@OnHandQty * Price)
                                              WHEN RowNumber = 1
                                              AND OnHand >= ReceiptQuantity THEN (ReceiptQuantity * Price)
                                              WHEN RowNumber = 1
                                              AND OnHand < ReceiptQuantity THEN (OnHand * Price)
                                         END
               ,@OnHandQty = OnHandQty = CASE
                                              WHEN RowNumber > 1
                                              AND @OnHandQty >= ReceiptQuantity THEN @OnHandQty - ReceiptQuantity
                                              WHEN RowNumber > 1
                                              AND @OnHandQty < ReceiptQuantity THEN 0
                                              WHEN RowNumber = 1
                                              AND OnHand >= ReceiptQuantity THEN (OnHand - ReceiptQuantity)
                                              WHEN RowNumber = 1
                                              AND OnHand < ReceiptQuantity THEN 0
                                         END/*,
                @PreviousProductID = ProductID*/
        FROM    #RawData WITH (TABLOCKX)
        OPTION (MAXDOP 1)
    

    Welp, this was the solution I ended up coming up with. I like to think the fine folks watching the #sqlhelp hashtag for pointing me to this article by Jeff Moden:

    http://www.sqlservercentral.com/articles/T-SQL/68467/

    I did end up having to use a Rownumber on the table because it wasn’t getting the first set of cases correctly. Using this construct I brought retrieiving the dataset down from 17 minutes, best I been able to do, to 12 Seconds on my vastly slower dev box. I’m confident production will lower that even more.

    I’ve tested the output and I get the exact same results as the old way except for when 2 items for the same product have different price and the update time is the exact same. One way may pick a different order then the other. It of 15,624 items that only happened once where the varience was >= a penny.

    Thanks everyone who answered here. I ultimately went a different way but I wouldn’t have found it without you.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.