I have a table with two fields: hospitalID (int) PK and internalID (int) FK. The problem is to update the internalid with a new value, that is gotten by finding the first available integer (minimum unused integer). Feel free to say this itself is a bad idea and suggest another.
So, I have this prototype code in VB6 (bear with me):
This works just fine with a single user. So, the client put it in production and had several people entering data on it, and it becomes clear that GetNewID sometimes returns the same ID to two different people. The first thought was to put GetNewID and the Execute method in a single transaction, but it's slow and we think it could be better done in a stored proc.
I'm thinking I need to take this code
and turn it into something like
. I have a few problems beyond not being sure of the best way to do that. First, I suspect it might be necessary to execute all of this code atomically to avoid concurrency issues, by enclosing it in a transaction. Is it? Next, I don't like the idea of sending a variable number of parameters to the stored proc, and I don't much like the idea of calling the proc multiple times, once for each hospitalid, either. Finally, the IN clause is light duty, and I'm wondering if there's a better way to do it. (20 different values would be atypically large, usually there are just a few values that change.) Is there a cool way of putting all this together in a stored proc?
TIA
An unforeseen consequence of the information revolution has been the exponential propagation of human error.
So, I have this prototype code in VB6 (bear with me):
Code:
newID = GetNewID() 'Iterates a recordset of logical ID's, and returns the first available one.
cmdString = "update hospid_intid set internalid = " & newID & " where hospitalid in ("
cmdString = cmdString & CStr(hospids(0))
For i = 1 To UBound(hospids)
cmdString = cmdString & ", " & CStr(hospids(i))
Next i
cmdString = cmdString & ")"
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = cn
.CommandText = cmdString
.Execute
End With
I'm thinking I need to take this code
Code:
update hospid_intid set internalid = " & newID & " where hospitalid in ("
cmdString = cmdString & CStr(hospids(0))
For i = 1 To UBound(hospids)
cmdString = cmdString & ", " & CStr(hospids(i))
Next i
cmdString = cmdString & ")"
Code:
update hospid_intid set internalid = ([COLOR=red] select whatever from whereever that gives me the next available ID--the minimum value that isn't already being used[/color]) where hospitalid in ([COLOR=red]@this, @that, @theother, @n...[/color])
TIA
An unforeseen consequence of the information revolution has been the exponential propagation of human error.