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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:43:35+00:00 2026-05-15T22:43:35+00:00

I need to write a procedure that will allow me to select x amount

  • 0

I need to write a procedure that will allow me to select x amount of rows and at the same time update those rows so the calling application will know those records are locked and in use. I have a column in the table named “locked”. The next time the procedure is called it will only pull the next x amount of records that do not have the “locked” column checked. I have read a little about the OUTPUT method for SQL server, but not sure that is what I want to do.

  • 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-15T22:43:35+00:00Added an answer on May 15, 2026 at 10:43 pm

    Vote for Cade Roux’s answer, using OUTPUT:

    UPDATE #tbl
       SET locked = 1
    OUTPUT INSERTED.*
     WHERE id IN (SELECT TOP 1 id
                    FROM #tbl
                   WHERE locked = 0
                ORDER BY id)​
    

    Previously:


    This is one of the few times I can think of using a temp table:

    ALTER PROCEDURE temp_table_test
    AS
    BEGIN
    
       SELECT TOP 5000 * 
         INTO #temp_test
         FROM your_table
        WHERE locked != 1
     ORDER BY ?
    
       UPDATE your_table
          SET locked = 1
        WHERE id IN (SELECT id FROM #temp_test)
    
       SELECT *
         FROM #temp_test
    
       IF EXISTS (SELECT NULL
                    FROM tempdb.dbo.sysobjects
                   WHERE ID = OBJECT_ID(N'tempdb..#temp_test'))
       BEGIN
         DROP TABLE #temp_test
       END
    
    END
    

    This:

    1. Fetches the rows you want, stuffs them into a local temp table
    2. Uses the temp table to update the rows to be “locked”
    3. SELECTs from the temp table to give you your resultset output
    4. Drops the temp table because they live for the session
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a stored procedure that will provide the data from two
I need to write an update script that will check to see if certain
I need to write some sql that will allow me to query all objects
I need to write a stored procedure that will search a table based on
I need to write a generic procedure that set value for one column or
I need a stored procedure which will allow me to return sorted results based
I'm trying to write a stored procedure in SQL Server that will eliminate some
I am trying to write a stored procedure that will check for the existence
I need to write a script that will prompt a database user in SQL+
i need to write a stored procedure which will return a string.logic is when

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.