Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Require serial access to table, Stored Procedure, TABLOCKX


Require serial access to table, Stored Procedure, TABLOCKX

Author
Message
munta
munta
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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>Wink
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.
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7001 Visits: 13559
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
munta
munta
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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.
Erin Ramsay
Erin Ramsay
Mr or Mrs. 500
Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)

Group: General Forum Members
Points: 561 Visits: 1099
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.
munta
munta
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search