DECLARE @MinValue INT , @MaxValue INT , @Quantity INT SELECT @MinValue = 10, @MaxValue = 100, @Quantity = 500 SELECT TOP (@Quantity) ABS(CHECKSUM(NEWID()))%(@MaxValue-@MinValue+1)+@MinValue FROM Master.sys.SysColumns sc1
select rnd, rnd, rnd, nid, nid, nid from (select rand() as rnd ,abs(checksum(newid()))%100 + 1 as nid ) as d;rnd rnd rnd nid nid nid---------------------- ---------------------- ---------------------- ----------- ----------- -----------0.455575000804869 0.455575000804869 0.455575000804869 54 54 54
select abs(checksum(newid()))%10 + 1 as nid1 ,abs(checksum(newid()))%10 + 1 as nid2 ,abs(checksum(newid()))%10 + 1 as nid3 ,abs(checksum(newid()))%10 + 1 as nid4 ,cast(rand()*10 as int)%10 + 1 as rnd1 ,cast(rand()*10 as int)%10 + 1 as rnd2 ,cast(rand()*10 as int)%10 + 1 as rnd3 ,cast(rand()*10 as int)%10 + 1 as rnd4nid1 nid2 nid3 nid4 rnd1 rnd2 rnd3 rnd4----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------8 10 6 4 2 3 10 7
DECLARE @MinValue INT , @MaxValue INT , @Quantity INT SELECT @MinValue = 10, @MaxValue = 100, @Quantity = 10 SELECT TOP (@Quantity) cast(rand()*(@MaxValue-@MinValue+1) as int)%(@MaxValue-@MinValue+1)+@MinValue as ConstRand ,ABS(CHECKSUM(NEWID()))%(@MaxValue-@MinValue+1)+@MinValue as NotConstNewID FROM Master.sys.SysColumns sc1ConstRand NotConstNewID----------- -------------88 6488 5988 7688 1188 4188 3888 2688 6388 8988 53
SELECT GETDATE() AS [GETDATE() (Always Same)], RAND() AS [RAND() (Always Same)], NEWID() AS [NEWID() (NEVER Same)] FROM Master..spt_Values WHERE Type = 'P' AND Number < 10