Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
Require serial access to table, Stored...
Require serial access to table, Stored Procedure, TABLOCKX
Rate Topic
Display Mode
Topic Options
Author
Message
doodledorf
doodledorf
Posted Saturday, February 23, 2013 1:40 AM
Grasshopper
Group: General Forum Members
Last Login: Tuesday, February 26, 2013 1:11 AM
Points: 10,
Visits: 27
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
LutzM
LutzM
Posted Saturday, February 23, 2013 2:49 AM
SSCertifiable
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 3:17 PM
Points: 6,731,
Visits: 12,131
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
doodledorf
doodledorf
Posted Saturday, February 23, 2013 12:40 PM
Grasshopper
Group: General Forum Members
Last Login: Tuesday, February 26, 2013 1:11 AM
Points: 10,
Visits: 27
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
Erin Ramsay
Erin Ramsay
Posted Monday, February 25, 2013 11:18 AM
SSC-Addicted
Group: General Forum Members
Last Login: Today @ 4:30 PM
Points: 421,
Visits: 776
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
doodledorf
doodledorf
Posted Tuesday, February 26, 2013 1:21 AM
Grasshopper
Group: General Forum Members
Last Login: Tuesday, February 26, 2013 1:11 AM
Points: 10,
Visits: 27
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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.