stainless
2013-02-13 08:37:00 UTC
We have an issue with locking data that is causing an integrity issue.
Our customers can create spatial information that require unique key identifiers on a SQL table. Please note that this is not an issue with whether or not to use an identity field, as this uniqiue key is a contsruct of a fiexed pice of text (i.e. "Pegasus") + the incremental number so as to support strict data rules imposed by embedded spatial database structures)
We have a table called Next_Num that holds the next value to be allocated to this key in a column called next_number and identified by a specific type value, in this case "Pegasus".
Our SQL is basically as follows, with @type in this case being set to "Pegasus".:
update next_num set next_number = next_number + 1 where type = @type
select @next_number = next_number - 1 from next_num
@next_number is output to the calling process to give the spatial data the unique key.
So basically we update immediately by incrementing the next_number on the table by 1. Then we select the new value from the table and subtract 1 to represent the value to be used. I know it seems a little convoluted with the subtraction but it has been like this for some time and at least should return the value that was on the table before the update.
However, we have thousands of customers accessing this process during the day, with some of their actions meaning they retrieve more than one value for some processes.
It appears that at some times, in between the update and the select, a second customer has also updated the table in that split second so 2 separate customers are getting the same @next_number value returned from the select statement. This creates data integrity issues.
The issue we have is finding a way of returning a @next_number value that reflects exactly the update applied for that customer without locking the whole table (as other processes are using the same table for different @type values with no issues).
We could, of course, have a dedicated table for just the "Pegasus" next number value which may help in some ways (allowing us to distance any solution from affecting other non-related key processes) but the issue is still there for this key. Select for Update seems to be the elegant method in later SQL Server versions but we are tied to SQl Server 2000. Any ideas as to how we can solve this?
Our customers can create spatial information that require unique key identifiers on a SQL table. Please note that this is not an issue with whether or not to use an identity field, as this uniqiue key is a contsruct of a fiexed pice of text (i.e. "Pegasus") + the incremental number so as to support strict data rules imposed by embedded spatial database structures)
We have a table called Next_Num that holds the next value to be allocated to this key in a column called next_number and identified by a specific type value, in this case "Pegasus".
Our SQL is basically as follows, with @type in this case being set to "Pegasus".:
update next_num set next_number = next_number + 1 where type = @type
select @next_number = next_number - 1 from next_num
@next_number is output to the calling process to give the spatial data the unique key.
So basically we update immediately by incrementing the next_number on the table by 1. Then we select the new value from the table and subtract 1 to represent the value to be used. I know it seems a little convoluted with the subtraction but it has been like this for some time and at least should return the value that was on the table before the update.
However, we have thousands of customers accessing this process during the day, with some of their actions meaning they retrieve more than one value for some processes.
It appears that at some times, in between the update and the select, a second customer has also updated the table in that split second so 2 separate customers are getting the same @next_number value returned from the select statement. This creates data integrity issues.
The issue we have is finding a way of returning a @next_number value that reflects exactly the update applied for that customer without locking the whole table (as other processes are using the same table for different @type values with no issues).
We could, of course, have a dedicated table for just the "Pegasus" next number value which may help in some ways (allowing us to distance any solution from affecting other non-related key processes) but the issue is still there for this key. Select for Update seems to be the elegant method in later SQL Server versions but we are tied to SQl Server 2000. Any ideas as to how we can solve this?