I tried to create a function to generate bigint
UID based on the timestamp from this solution. While accuracy should be sufficient to avoid collisions, the server obviously does not measure the datetime2
returned by sysdatetime()
each time it is called. In a loop, I am able to generate dozens of identical UIDs before a change occurs. I'm not sure exactly how it works inside, but is there any way to force the generation of a new datetime2
with each call?
create function [func].GenerateBigIntID()
returns bigint
as
begin
declare @Binary binary(9) = cast(reverse(cast(sysdatetime() as binary(9))) as binary(9))
return(select cast(substring(@Binary, 1, 3) as bigint) * 864000000000 + cast(substring(@Binary, 4, 5) as bigint))
end