Record Locking

Last post 10-06-2008 9:46 AM by Luis Martin. 3 replies.
Page 1 of 1 (4 items)
Active Topics My Discussions Unanswered Sort Posts: Previous Next
  • 10-06-2008 8:32 AM

    • kitz
    • Not Ranked
    • Joined on 10-06-2008
    • Posts 2

    Record Locking

    Hi there, I'm developing an VB6 app that uses SQL occasionally, and I have this question: I have a table that one if its fields increments with each update. Once the new value is updated (incremented), I "select" it to the VB6 app. What we're afraid of is, what will happen if inbetween the "update" and "select", a different user will "select" it, and the two users will get the identical value. How should I go about locking this record for the duration of the two DB calls?

    (In short, I need to do this:)
    ------------------------------------
    update drafts set counter = counter + 1 where id =...
    select counter from drafts where id =...
    ------------------------------------
    I want to lock that record before the "update", and unlock it right after the "select"

  • 10-06-2008 9:19 AM In reply to

    Re: Record Locking

    Welcome to the forum! If you don't use NOLOCK in your select then, for sure, the first user (update or select) will lock the table.
    Luis Martin
    Moderator
    SQL-Server-Performance.com

    When the power of love overcomes the love of the power, the world will know peace.

    J. Hendrix


    All postings are provided “AS IS” with no warranties for accuracy.
  • 10-06-2008 9:31 AM In reply to

    • kitz
    • Not Ranked
    • Joined on 10-06-2008
    • Posts 2

    Re: Record Locking

    Thank you Luis, but can I use this lock before the "update" ?
  • 10-06-2008 9:46 AM In reply to

    Re: Record Locking

    If you want to run a select without locking the table, then use NOLOCOK. Check BOL for use this option.
    Luis Martin
    Moderator
    SQL-Server-Performance.com

    When the power of love overcomes the love of the power, the world will know peace.

    J. Hendrix


    All postings are provided “AS IS” with no warranties for accuracy.
Page 1 of 1 (4 items)
Active Topics   My Discussions    Unanswered Posts


© 2000 - 2007 vDerivatives Limited All Rights Reserved.