Require serial access to table, Stored Procedure, TABLOCKX

  • Hi all. Database newbie.

    I have a one off requirement to rename all our computers on the domain. (hundreds)

    Part of the naming convention is a unique number beginning with 0001 and increasing to however many machines we have.

    Steps will be as follows:

    1.) At computer shutdown a local script (via GPO) will access SQL Server sending the current computers name as input to a stored procedure.

    2.) Stored Procedure inserts computers current name into table X (with an auto incrementing identity column as primary key).

    3.) The identity is gathered from the insert (SELECT SCOPE_IDENTITY()).

    4.) A new computer name is created by the stored procedure using the returned identity value as part of the equation.

    5.) The record that was inserted now has a column updated with the newly generated computer name. (<identity value>,<old computerName>,<new computerName>)

    6.) The newly generated computer name is returned to the calling script.

    7.) Computer is renamed by the script and shutdown completes.

    What I need to know is how to guarantee that the identity value selected immediately after the insert is indeed the one from the insert in question?

    If two computers attempt the workflow at the same time (unlikely but possible) it is vital that the identity value returned is not from another insert.

    The reason is that the records entered into the database will be used to manually alter another DNS registration database we have no administrative access to or control of.

    Each <old computerName> value in the registration database will be updated to the <new computerName> value and hence must exactly match the actual computer (host) name of the associated computer.

    Do I need to lock the table while I carry out the insert and update work or does scope_identity() always return the correct identity value for the instance of the stored procedure that ran the insert.

    I hope the above makes some semblance of sense and someone can offer some advice based on experience.

    Jason.

  • You could use the OUTPUT clause in the INSERT statement and capture the primary key together with the values inserted in a table variable.

    See BOL (BooksOnLine) for an example (keyword OUTPUT).

    Another solution could be a computed column that would generate the new name. That way you wouldn't even have to worry about the additional update statement...



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • LutzM.

    Thank you very much for the reply. After some investigation I believe combining both computed column and output to retrieve the newly computed "name" in theory should work very well. I will carry out some testing next week and post back if I have any issues.

    Jason.

  • SCOPE_IDENTITY() returns the identity value generated in the table in the current session.

    ie., I would assume that each time you call the procedure it would be a new session and even if you have concurrent calls by different computers they would be limited by scope and session and would not pull cross identities.

  • Ok, that is reassuring to know. Not sure what path I am going to take with this as it is still in the concept stage.

    Thanks for the reply.

    Jason.

Viewing 5 posts - 1 through 4 (of 4 total)

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