Database-wide unique-yet-simple identifiers in SQL Server

后端 未结 11 824
余生分开走
余生分开走 2020-12-12 12:50

First, I\'m aware of this question, and the suggestion (using GUID) doesn\'t apply in my situation.

I want simple UIDs so that my users can easily communicate this i

11条回答
  •  没有蜡笔的小新
    2020-12-12 13:10

    We faced a similar problem on a project. We solved it by first creating a simple table that only has one row: a BIGINT set as auto-increment identity. And we created an sproc that inserts a new row in that table, using default values and inside a transaction. It then stores the SCOPE_IDENTITY in a variable, rolls back the transaction and then returns the stored SCOPE_IDENTITY.

    This gives us a unique ID inside the database without filling up a table.

    If you want to know what kind of object the ID is referring to, I'd lose the transaction rollback and also store the type of object along side the ID. That way findout out what kind of object the Id is referring to is only one select (or inner join) away.

提交回复
热议问题