SELECT and UPDATE Data

  • Hi All,

    We have a table which have high data insert with a status flag column , I want to select data having status=1 and then update the status to 2. The table get inserted very frequently and now what my procedure does is like below.
    create procedure gettext1
    as begin
    set nocount on;
      UPDATE [dbo].[communication1] set status1=2 where status1=1

    SELECT text1
      FROM [dbo].[communication1] with (nolock) where status1=2
    UPDATE [dbo].[communication1] set status1=3 where status1=2
    end

    How can I make this more efficient as I feel this is not the right approach.

  • Rechana Rajan - Sunday, March 3, 2019 11:43 PM

    Hi All,

    We have a table which have high data insert with a status flag column , I want to select data having status=1 and then update the status to 2. The table get inserted very frequently and now what my procedure does is like below.
    create procedure gettext1
    as begin
    set nocount on;
      UPDATE [dbo].[communication1] set status1=2 where status1=1

    SELECT text1
      FROM [dbo].[communication1] with (nolock) where status1=2
    UPDATE [dbo].[communication1] set status1=3 where status1=2
    end

    How can I make this more efficient as I feel this is not the right approach.

    Use the OUTPUT clause and change status1 from 1 to 3:

    UPDATE [dbo].[communication1] SET status1 = 3
    OUTPUT DELETED.text1
    WHERE status1 = 1

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply