--如果樓主不覺得麻煩,可以試試這個. declare @t table(col varchar(01)) insert into @t select 'a' union select 'b' union select 'c' union select 'd' union select 'e' union select 'f' union select 'g' union select 'h' union select 'i' union select 'j' union select 'k' union select 'l' ....union select '8' union select '9'declare @sql varchar(10) set @sql='' select @sql=@sql+col from (select top 5 * from @t order by newid()) A print @sql
declare @s varchar(36) set @s='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' print substring(@s,cast(rand()*36 as int)%36+1,1) +substring(@s,cast(rand()*36 as int)%36+1,1) +substring(@s,cast(rand()*36 as int)%36+1,1) +substring(@s,cast(rand()*36 as int)%36+1,1) +substring(@s,cast(rand()*36 as int)%36+1,1)
只需要字母: declare @s varchar(26) set @s='ABCDEFGHIJKLMNOPQRSTUVWXYZ' print substring(@s,cast(rand()*26 as int)%26+1,1) +substring(@s,cast(rand()*26 as int)%26+1,1) +substring(@s,cast(rand()*26 as int)%26+1,1) +substring(@s,cast(rand()*26 as int)%26+1,1) +substring(@s,cast(rand()*26 as int)%26+1,1)
把5作为一个可变参数输入 declare @count int,@l_rand int,@l_str varchar(8),@flag int set @count = 5 set @l_str = '' set @flag = 1 while @flag <= @count begin set @l_rand = abs(checksum(newid()) % 37) set @l_str = @l_str + char(@l_rand + case when @l_rand<=10 then 48 when @l_rand>10 then 87 end) set @flag = @flag + 1 end select @l_str
楼主要得到字母的话用playwarcraft(三角褲叉叉的頂點) 和 pbsql(风云) 的都可以风云的可改进一下,取随机大小写 declare @s char(52) set @s='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' print substring(@s,cast(rand()*52 as int)%52+1,1) +substring(@s,cast(rand()*52 as int)%52+1,1) +substring(@s,cast(rand()*52 as int)%52+1,1) +substring(@s,cast(rand()*52 as int)%52+1,1) +substring(@s,cast(rand()*52 as int)%52+1,1)
declare @t table(col varchar(01))
insert into @t
select 'a' union select 'b' union select 'c' union select 'd' union select 'e' union select 'f' union select 'g' union select 'h' union select 'i' union select 'j' union select 'k' union select 'l' ....union select '8' union select '9'declare @sql varchar(10)
set @sql=''
select @sql=@sql+col from (select top 5 * from @t order by newid()) A
print @sql
set @s='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print substring(@s,cast(rand()*36 as int)%36+1,1)
+substring(@s,cast(rand()*36 as int)%36+1,1)
+substring(@s,cast(rand()*36 as int)%36+1,1)
+substring(@s,cast(rand()*36 as int)%36+1,1)
+substring(@s,cast(rand()*36 as int)%36+1,1)
declare @s varchar(26)
set @s='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print substring(@s,cast(rand()*26 as int)%26+1,1)
+substring(@s,cast(rand()*26 as int)%26+1,1)
+substring(@s,cast(rand()*26 as int)%26+1,1)
+substring(@s,cast(rand()*26 as int)%26+1,1)
+substring(@s,cast(rand()*26 as int)%26+1,1)
declare @count int,@l_rand int,@l_str varchar(8),@flag int
set @count = 5
set @l_str = ''
set @flag = 1
while @flag <= @count
begin
set @l_rand = abs(checksum(newid()) % 37)
set @l_str = @l_str + char(@l_rand + case when @l_rand<=10 then 48 when @l_rand>10 then 87 end)
set @flag = @flag + 1
end
select @l_str
declare @s char(52)
set @s='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
print substring(@s,cast(rand()*52 as int)%52+1,1)
+substring(@s,cast(rand()*52 as int)%52+1,1)
+substring(@s,cast(rand()*52 as int)%52+1,1)
+substring(@s,cast(rand()*52 as int)%52+1,1)
+substring(@s,cast(rand()*52 as int)%52+1,1)