• subscribe
October 23, 2002 12:00 AM

Confining an Incremental Counter Value to One Table

SQL Server Pro
InstantDoc ID #26854
Downloads
26854.zip

How can I lock a row in a SQL Server database to simulate the IDENTITY property column? I want to guarantee that two rows don't contain the same number.

On your table, you can take an exclusive lock that confines the next incremental counter value to that table. Listing 2 shows sample code that takes such a lock. Be sure, however, not to call your number-allocation routine within a larger transaction because the c1 value will be locked until the outer transaction commits or aborts.

Obtaining a new number before starting the main transaction is typical. However, you might have to handle sequence gaps if the second transaction never commits because some business rules don't allow sequence gaps. If you have to accommodate sequence gaps, insert a dummy record into the table. You can replace the dummy record after the transaction is successfully committed.



ARTICLE TOOLS

Comments
  • BrendonJ
    10 years ago
    Dec 01, 2002

    I can't see how this returns a the next unique number to use for your insert. Dosen't it just re-seed all the values in the column by an increment of 1?
    I'm guessing that mycounter is the table name and C1 is the column.
    Could somebody show me the light?

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...