Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Require serial access to table, Stored Procedure, TABLOCKX Expand / Collapse
Author
Message
Posted Saturday, February 23, 2013 1:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 7, 2013 1:44 PM
Points: 11, Visits: 37
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.
Post #1423326
Posted Saturday, February 23, 2013 2:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 4:22 PM
Points: 7,161, Visits: 13,234
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
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1423331
Posted Saturday, February 23, 2013 12:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 7, 2013 1:44 PM
Points: 11, Visits: 37
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.
Post #1423370
Posted Monday, February 25, 2013 11:18 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, August 28, 2014 2:20 PM
Points: 541, Visits: 1,047
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.
Post #1423715
Posted Tuesday, February 26, 2013 1:21 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 7, 2013 1:44 PM
Points: 11, Visits: 37
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.
Post #1423921
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse