服務升級需為舊資料產生 `UniqueId`(`long` 型別),要求單機唯一、可排序、可作索引。採用 `DateTime.UtcNow.Ticks` 邏輯:從 0001/1/1 00:00:00 起的 100 奈秒間隔數。
DECLARE @current_datetime DATETIME = GETUTCDATE();
DECLARE @days_from_0001_to_1900 BIGINT = 693595; -- 0001/1/1 到 1900/1/1 天數
DECLARE @ticks_per_ms BIGINT = 10000;
DECLARE @ticks_per_day BIGINT = 24*3600*1000*@ticks_per_ms;
DECLARE @ms_of_day BIGINT = DATEPART(MILLISECOND, @current_datetime)
+ DATEPART(SECOND, @current_datetime)*1000
+ DATEPART(MINUTE, @current_datetime)*60000
+ DATEPART(HOUR, @current_datetime)*3600000;
DECLARE @ticks_of_days BIGINT = CAST(@days_from_0001_to_1900 + DATEDIFF(DAY, 0, @current_datetime) AS BIGINT) * @ticks_per_day;
DECLARE @ticks_of_time BIGINT = @ms_of_day * @ticks_per_ms;
DECLARE @ticks BIGINT = @ticks_of_days + @ticks_of_time;
SELECT @current_datetime, @ticks;
C# 端 `new DateTime(2014,4,1,2,52,54,153, DateTimeKind.Utc).Ticks` 得 `635319175741530000`,T-SQL 同步產出完全一致(圖 1-2)。


可直接用於資料遷移腳本批量產生 `UniqueId`。