创建存储过程可以解决此问题

1 1 Create PROCEDURE [dbo].[Test] 2 2 @Len int,--生成字符串的长度 3 3 @Type int--生成字符串的类型 1:全数字, 2:全大写字母,3:全小写字母,4:数字+大写字母+小写字母组合 4 4 AS 5 5 BEGIN 6 6 declare @rand1 int;--用于判断是获取字符串类型 1:数字,2:大写字母,3:小写字母,4:数字/大写字母/小写字母 7 7 declare @rand2 int;--用户获取随机数,拼接字符串 8 8 declare @rand int;--拼接后转为ASCII的数字 9 9 declare @returnStr nvarchar(100); set @returnStr='' 1010 1111 if(@Type=1) begin set @rand1=1 end; 1212 if(@Type=2) begin set @rand1=2 end; 1313 if(@Type=3) begin set @rand1=3 end; 1414 while(@Len>0) 1515 begin 1616 if(@Type=4) 1717 begin 1818 --RAND() 生成随机0到1之间的数字 1919 --CEILING() 向上取整 2020 set @rand1=CONVERT(INT, CEILING(RAND()*3)) 2121 end; 2222 --FLOOR() 向下取整 2323 set @rand2= CONVERT(INT, FLOOR(RAND()*26)); 2424 set @rand=case @rand1 when 1 then CONVERT(INT, FLOOR(RAND()*10))+48 2525 when 2 then @rand2+ 65 2626 else @rand2+ 97 end 2727 set @returnStr=@returnStr+char(@rand); 2828 set @Len=@Len-1 2929 end 3030 select @returnStr as randstr into #temp 3131 select * from #temp 3232 END 3333 3434 3535 --select char(48),char(49),char(57) ---48-57 (数字) 10个 3636 --select char(65),char(66),char(90) --65-90 (大写) 26个 3737 --select char(97),char(98),char(122) --97-122 (小写) 26个
View Code