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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:51:01+00:00 2026-06-15T23:51:01+00:00

Primary Question: I want to truncate and refresh a table in SQL Server, but

  • 0

Primary Question:

I want to truncate and refresh a table in SQL Server, but want to wait until any queries currently accessing the table to finish. Is this a simple setting in SQL Server, or do I need to create some logic to accomplish it?

Detailed Description:

I have a VB application that sits on about 300 terminals. The application calls a SqlServer(2008 R2) stored procedure ([spGetScreenData]) every 2 minutes to get the latest sales data.
[spGetScreenData] creates a series of temp tables and returns a select query of about 200 rows and 100 columns. It takes about 8 seconds to execute.

My goal is to create a new stored procedure ([spRefreshScreenData]) that executes every two minutes which will refresh the data in a table ([SCREEN_DATA]). I will then change [spGetScreenData] to simply query [SCREEN_DATA].

The job that refreshes [SCREEN_DATA] first sets a flag in a status table to ‘RUNNING’ while it executes. Once complete, it sets that status to ‘COMPLETED’.

[spGetScreenData] checks the status of the flag before querying and waits (for a period of time) until it’s ready. Something like…

DECLARE @Condition AS BIT=0
,       @Count AS INT=0
,       @CycleCount AS INT=10 --10 cycles (20 Seconds)

WHILE @Condition = 0 AND @Count < @CycleCount
BEGIN
    SET @Count = @Count + 1

    IF EXISTS(  SELECT  Status 
                FROM    tbl_Process_Status
                WHERE   Process = 'POS_Table_Refresh' 
                AND     Status='Running')
            WAITFOR DELAY '000:00:02' --Wait 2 seconds
        ELSE
            SET @Condition=1
END

SELECT *
FROM SCREEN_DATA
WHERE (Store=@Store OR @Store IS NULL)

My concern has to do with [spRefreshScreenData]. When [spRefeshScreenData] begins its truncation, there could be dozens of requests for the data currently running.

Will SqlServer simply wait until the request are done before truncating? Is there a setting I have to set to not mess these queries up?

Or do I have to build some mechanism to wait until all requests are completed before starting the truncation?

  • 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-15T23:51:02+00:00Added an answer on June 15, 2026 at 11:51 pm

    The job that refreshes [SCREEN_DATA] first sets a flag in a status table to ‘RUNNING’ while it executes. Once complete, it sets that status to ‘COMPLETED’.

    [spGetScreenData] checks the status of the flag before querying and waits (for a period of time) until it’s ready

    Don’t. Use app locks. The readers (spGetScreenData) are the app lock in shared mode, the writers (refresh job) requests it X mode. See sp_getapplock.

    But even this is no necessary. You can build the new data online, while the queries continue, w/o affecting them using a staging table, ye a different table than the one queried by the apps. When the rebuild is complete simply swap the original tabel with the staging one, using either fast SWITCH operations (see Transferring Data Efficiently by Using Partition Switching) or using the good ‘ole sp_rename trick.

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

Sidebar

Related Questions

Similar to This Question using linq to SQL , but I don't want to
This seems to be primary Question. but i search it and i couldn't find
Slightly more advanced mapping then in my previous question :) Tables: create table [Primary]
I have quick question for you SQL gurus. I have existing tables without primary
I have a simple SQL question. I want to make a 3 column database
I have a table with INT primary key column. I do not want to
Primary Question What I want to do is fairly easy. Or so you would
Probably a trivial question, but I want to get the best possible solution. Problem:
This is a basic database design question. I want a table (or multiple tables)
I need help with a database table called Question. I want to know which

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.