日期:2014-05-18 浏览次数:20562 次
--参考: declare @maxNumber int, --随机数最大值 @minNumber int, --随机数最小值 @rows int --要取得的行数 select @maxNumber=10000, @minNumber=10000, @rows=10 set rowcount @rows select distinct '1234 4567 '+convert(varchar,ID)+' '+convert(varchar,ID)+' '+convert(varchar,ID)+' '+convert(varchar,ID) as 'B' from ( select convert(int,rand(checksum(newid()))*@minNumber) as ID from syscolumns,sysobjects )t set rowcount 0 ----------------------------------- declare @num1 int ,@num2 int ,@num3 int ,@num4 int set @num1=rand(abs(convert(int,checksum(newid()))))*10000 set @num2=rand(abs(convert(int,checksum(newid()))))*10000 set @num3=rand(abs(convert(int,checksum(newid()))))*10000 set @num4=rand(abs(convert(int,checksum(newid()))))*10000 select convert(varchar(100),@num1)+' '+convert(varchar(100),@num2)+' '+convert(varchar(100),@num3)+' '+convert(varchar(100),@num4) --------------------------------------- declare @r1 numeric (15,0),@r2 numeric (15,0) SELECT @r1=RAND( (DATEPART(mm, GETDATE()) * 100000 ) + (DATEPART(ss, GETDATE()) * 1000 ) + DATEPART(ms, GETDATE()) )*10000 print @r1
------解决方案--------------------
--把2楼的写成个函数 --见newid视图 create view v_newid as select [id] = newid() --建函数,获取随即9位数据+字母密码 create function f_getRandPassword9() returns varchar(9) as begin declare @result varchar(9) select @result = right(id,9) from v_newid; return @result; end --查询结果 select dbo.f_getRandPassword9() --结果 /* --------- 357771BF6 (1 行受影响) */
------解决方案--------------------
select right(newid(),9) 最好,而且简单
------解决方案--------------------
可能重复的几率有多大呀。 很少遇到重复,不信你试试。
------解决方案--------------------