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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:55:39+00:00 2026-06-08T19:55:39+00:00

I have a pending order table with a check constraint to prevent people from

  • 0

I have a pending order table with a check constraint to prevent people from ordering an item we don’t have in stock. This required me to create a counter function to decide if an insert can happen or not. It works until there is 1 item left in inventory then I get a message that we are out of stock of the item. I thought it was a dirty read issue but even after interducing a ReadPast hint I still see this behavior. Is there some other factor causing this problem? Or do I need to setup the isolation level differently?

I have tried calling this function with the sprokID and it returns true which is why I am thinking during insert there is a dirty read taking place.

ALTER TABLE [dbo].[PendingSprokOrders]  WITH CHECK ADD  CONSTRAINT [CK_SprokInStock] CHECK  (([dbo].[SprokInStockCount]([SprokID])=(1)))

FUNCTION [dbo].[SprokInStockCount] ( @SprokId INT )
RETURNS INT
AS 
    BEGIN 

        DECLARE @Active INT
        SET @Active = ( SELECT  COUNT(*)
                        FROM    [PendingSprokOrders] AS uac   WITH(READPAST)
                        WHERE   uac.SprokID = @SprokId
                      ) 
        DECLARE @Total INT
        SET @Total = ( SELECT
                                ISNULL(InStock, 0)
                       FROM     SprokInvetory
                       WHERE    id = @SprokId
                     )


        DECLARE @Result INT

        IF @Total - @Active > 0 
            SET @Result = 1
        ELSE 
            SET @Result = 0  
        RETURN @Result;
    END;
  • 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-08T19:55:40+00:00Added an answer on June 8, 2026 at 7:55 pm

    The math is off. Instead of:

        IF @Total - @Active > 0 
            SET @Result = 1
        ELSE 
            SET @Result = 0  
    

    it should be:

        IF @Total - @Active > -1 
            SET @Result = 1
        ELSE 
            SET @Result = 0  
    

    That’s because your constraint function can see the row that you are attempting to add and is counting it.

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

Sidebar

Related Questions

I have a date field in the database table of this format 2012-02-1.i need
I have a table full of orders, where each order has a state (for
If you have a pending operation, eg stream.BeginRead(_buffer, 0, _buffer.Length, _asyncCallbackRead, this); and you
I have an app with a pending intent subscribed to ACTION_NDEF_DISCOVERED and a normal
I have a multi-R/W lock class that keeps the read, write and pending read
have written this little class, which generates a UUID every time an object of
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have a painfully simple blog Post creator, and I'm trying to check if the
I have this mysql query using PHP and first part is working fine, but
I have a table called user_relationship. which has two foreign keys refering back to

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.