You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlserver.sql 2.0 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. ˵
  3. 1.SQL Serverһ÷д˽ű󣬽SQL Serverġɱ--ֵһ Fn_NextSnowId
  4. 2.ɵID = ʱ + WokerId +
  5. ʱ = ǰʱ뵥λ - 1582136402000
  6. WorkerId = {ֵ}
  7. = 5 2^SeqBigLength-1 ֮
  8. 3.÷
  9. 磺select dbo.Fn_NextSnowId(rand())
  10. ˵ dbo. ǰ׺
  11. 4.Զֵ
  12. ΪѩIDͣbigintԽ Ĭֵ󶨡 Ϊ ([dbo].[Fn_NextSnowId](rand()))ԱInsert¼ʱܸԶֵⲿ롣
  13. */
  14. -- ɾ
  15. if exists(select * from sys.objects where name='Fn_NextSnowId')
  16. drop function dbo.Fn_NextSnowId;
  17. Go
  18. -- ٴ
  19. CREATE function dbo.Fn_NextSnowId
  20. (
  21. @RandomSeed float -- IDĺҪһڵʱϵͳ rand()
  22. )
  23. returns bigint
  24. as
  25. begin
  26. declare @CurrentTime bigint
  27. declare @TimeTick bigint
  28. declare @WorkerId int
  29. declare @WorkerIdBigLength int
  30. declare @SeqBigLength int
  31. -- Begin: ҪʼIJȷ @WorkerIdBigLength @SeqBigLength ֵӦóͬ
  32. set @WorkerId = 1 -- ֵ 2^@WorkerIdBigLength-1
  33. set @WorkerIdBigLength = 4 -- Լ@WorkerIdBigLength+@SeqBigLength<23
  34. set @SeqBigLength = 8 -- 鲻С6ڵǰSQL汾У@SeqBigLength ֵδҪݱ¼Seqֵ
  35. -- End
  36. -- ǰʱ뵥λ
  37. set @CurrentTime = CONVERT(BIGINT,DATEDIFF(MI,'1970-01-01 00:00:00.000', GETUTCDATE())) * 60000 + DATEPART(S,GETUTCDATE()) * 1000 + DATEPART(MS, GETUTCDATE())
  38. -- õǰʱȥʱ䣬óIDʱ
  39. set @TimeTick=@CurrentTime-1582136402000
  40. -- ID
  41. -- ѩID = 52^SeqBigLength-1֮ (5 + round((POWER(2, @SeqBigLength)-1) * rand(), 0)
  42. return @TimeTick * POWER(2, @WorkerIdBigLength + @SeqBigLength) + @WorkerId * POWER(2, @SeqBigLength) + (5 + round((POWER(2, @SeqBigLength)-1) * @RandomSeed, 0))
  43. end
  44. GO