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.