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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:58:14+00:00 2026-05-18T10:58:14+00:00

SQL Server 2008: I’ve got a situation here in which I wish to read

  • 0

SQL Server 2008: I’ve got a situation here in which I wish to read from a table and write a row under certain conditions. The problem is that I don’t want another request coming it at exactly the same time and doing the same thing. I’ll try to explain here:

Table Name: RequestQueue

Columns:

RequestID, StartDate, EndDate, RequestResult

Sample Data

1, 12/4/10 1:00pm, 12/4/10 1:02pm, Success
2, 12/4/10 1:04pm, 12/4/10 1:05pm, Success
3, 12/4/10 1:00pm, NULL, NULL

When a page loads in my app, I want it to look at this table and if there is a request still pending like (ID #3) it will not do anything. Otherwise, if there are no requests pending, it creates a new row with the ID and StartDate filled in.

The issue is that we could get into a situation where the page is loaded twice at almost exactly the same time. If they happen to both read from the table before the new row is produced, then I could get two new rows in there. I want to have some sort of query that reads from the table and if there are no requests pending, inserts the new row with the StartDate filled in. I want that query to run all the way before another page can even read from this table so I don’t get the “double row” effect.

I might need “locking” or something, I googled that but haven’t found something for my exact situation. I’m sure this could be a simple stored procedure I just need a push in the right direction here.

Thanks,

Robert

  • 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-05-18T10:58:14+00:00Added an answer on May 18, 2026 at 10:58 am

    Assuming that you just want to block all concurrent access to the table you could just do.

    BEGIN TRAN 
    
    DECLARE @StartDate datetime,
            @EndDate datetime
    
    SELECT TOP 1
                @StartDate = StartDate,
                @EndDate = EndDate
            FROM     RequestQueue WITH(TABLOCK,XLOCK)
            ORDER BY RequestID DESC
    
    
    IF @EndDate IS NULL
      SELECT @StartDate AS 'StartDate'
    ELSE
      INSERT INTO RequestQueue (StartDate) 
      OUTPUT INSERTED.* /* Or use SCOPE_IDENTITY() instead*/
      VALUES (GETDATE())
    
    COMMIT
    

    Alternatively you could just serialise access to the SELECT/INSERT code inside the specific procedure without taking an exclusive table lock by using sp_getapplock

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

Sidebar

Related Questions

No related questions found

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.