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

  • Home
  • SEARCH
  • 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 62633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:30:23+00:00 2026-05-10T18:30:23+00:00

I was recently tasked with debugging a strange problem within an e-commerce application. After

  • 0

I was recently tasked with debugging a strange problem within an e-commerce application. After an application upgrade the site started to hang from time to time and I was sent in to debug. After checking the event log I found that the SQL-server wrote ~200 000 events in a couple of minutes with the message saying that a constraint had failed. After much debugging and some tracing I found the culprit. I’ve removed some unnecessary code and cleaned it up a bit but essentially this is it

WHILE EXISTS (SELECT * FROM ShoppingCartItem WHERE ShoppingCartItem.PurchID = @PurchID) BEGIN     SELECT TOP 1          @TmpGFSID = ShoppingCartItem.GFSID,          @TmpQuantity = ShoppingCartItem.Quantity,         @TmpShoppingCartItemID = ShoppingCartItem.ShoppingCartItemID,     FROM         ShoppingCartItem INNER JOIN GoodsForSale on ShoppingCartItem.GFSID = GoodsForSale.GFSID     WHERE ShoppingCartItem.PurchID = @PurchID      EXEC @ErrorCode = spGoodsForSale_ReverseReservations @TmpGFSID, @TmpQuantity     IF @ErrorCode <> 0     BEGIN         Goto Cleanup         END      DELETE FROM ShoppingCartItem WHERE ShoppingCartItem.ShoppingCartItemID = @TmpShoppingCartItemID     -- @@ROWCOUNT is 1 after this END 

Facts:

  1. There’s only one or two records matching the first select-clause
  2. RowCount from the DELETE statement indicates that it has been removed
  3. The WHILE-clause will loop forever

The procedure has been rewritten to select the rows that should be deleted into a temporary in-memory table instead so the immediate problem is solved but this really sparked my curiosity.

Why does it loop forever?

Clarification: The delete doesn’t fail (@@rowcount is 1 after the delete stmt when debugged) Clarification 2: It shouldn’t matter whether or not the SELECT TOP … clause is ordered by any specific field since the record with the returned id will be deleted so in the next loop it should get another record.

Update: After checking the subversion logs I found the culprit commit that made this stored procedure to go haywire. The only real difference that I can find is that there previously was no join in the SELECT TOP 1 statement i.e. without that join it worked without any transaction statements surrounding the delete. It appears to be the introduction of the join that made SQL server more picky.

Update clarification: brien pointed out that there’s no need for the join but we actually do use some fields from the GoodsForSale table but I’ve removed them to keep the code simply so that we can concentrate on the problem at hand

  • 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. 2026-05-10T18:30:24+00:00Added an answer on May 10, 2026 at 6:30 pm

    Are you operating in explicit or implicit transaction mode?

    Since you’re in explicit mode, I think you need to surround the DELETE operation with BEGIN TRANSACTION and COMMIT TRANSACTION statements.

    WHILE EXISTS (SELECT * FROM ShoppingCartItem WHERE ShoppingCartItem.PurchID = @PurchID) BEGIN     SELECT TOP 1              @TmpGFSID = ShoppingCartItem.GFSID,              @TmpQuantity = ShoppingCartItem.Quantity,             @TmpShoppingCartItemID = ShoppingCartItem.ShoppingCartItemID,     FROM             ShoppingCartItem INNER JOIN GoodsForSale on ShoppingCartItem.GFSID = GoodsForSale.GFSID     WHERE ShoppingCartItem.PurchID = @PurchID      EXEC @ErrorCode = spGoodsForSale_ReverseReservations @TmpGFSID, @TmpQuantity     IF @ErrorCode <> 0     BEGIN             Goto Cleanup         END      BEGIN TRANSACTION delete          DELETE FROM ShoppingCartItem WHERE ShoppingCartItem.ShoppingCartItemID = @TmpShoppingCartItemID         -- @@ROWCOUNT is 1 after this      COMMIT TRANSACTION delete END 

    Clarification: The reason you’d need to use transactions is that the delete doesn’t actually happen in the database until you do a COMMIT operation. This is generally used when you have multiple write operations in an atomic transaction. Basically, you only want the changes to happen to the DB if all of the operations are successful.

    In your case, there’s only 1 operation, but since you’re in explicit transaction mode, you need to tell SQL Server to really make the changes.

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

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This will turn off notices for the environment programmatically-- from… May 11, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer I use the --mirror option and push to a personal… May 11, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer int* writePosition = (int* ) &buffer2[5] Or *((int*) &buffer2[10] )… May 11, 2026 at 6:45 pm

Related Questions

I was recently tasked with coming up with an offsite backup strategy. We have
I have been tasked with developing a solution that tracks changes to a database.
We have an auto-complete list that's populated when an you send an email to
Recently I asked a question about how to clean up what I considered ugly

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.