sqlserver 生成100万个不重复的10位随机数存入数据库

USE tempdb
GO
CREATE TABLE RandomNumber(Id char(10),IsUsed bit not null)
CREATE UNIQUE INDEX IX_RandomNumber ON RandomNumber(Id)
WITH IGNORE_DUP_KEY
GO
DECLARE @dt datetime
SET @dt = GETDATE()
SET NOCOUNT ON
DECLARE @row int
SET @row = 1000000
WHILE @row > 0
BEGIN
RAISERROR( 'need %d rows ', 10, 1, @row) WITH NOWAIT
SET ROWCOUNT @row
INSERT RandomNumber SELECT
Id = RIGHT(10000000000 + CONVERT(bigint, ABS(CHECKSUM(NEWID()))), 10)
,IsUsed=0
FROM syscolumns c1, sysobjects o--, syscolumns c2

SET @row = @row - @@ROWCOUNT
END
SELECT BeginDate = @dt, EndDate = GETDATE(), Second = DATEDIFF(Second, @dt, GETDATE())
GO
SELECT COUNT(*) FROM RandomNumber
GO


作者: alen88   发布时间: 2010-09-16