Generate a unique time-based id on a table in SQL Server

前端 未结 2 1890
不知归路
不知归路 2020-12-22 08:42

I\'m trying to generate a unique id/timestamp based on the clock of the database server. This is not the main Id of the table, but it\'s in my interest that this value is un

2条回答
  •  滥情空心
    2020-12-22 09:26

    Don't do it. Create a numerical identity column. For the most part, that will keep track of the order of inserts (this gets a little tricky in multi-threaded environments, but it should be close enough).

    Add another column, say createdAt with a default value of the timestamp. You can make this a datetime2 column for more precision.

    If you really need a unique numerical value, I would suggest creating a computed column whose type is something like decimal(38, 10). It would have a structure something like this: YYYYMMDDHHMMSSFFFFFF.. ("F" is for fractional seconds.)

提交回复
热议问题