说明:生成一个自定义位数的密码,密码可能由以下元素随机组合而成
一:0,1,2,3,4,5,6,7,8,9
二:a到z(小写)
三:A到Z(大写)
四:!,@,#,$,^,&,*,(,),_,+
使用方法:select(print) dbo.getpwd(密码位数)
Code
CREATE FUNCTION GetPwd(@count int)
RETURNS varchar(8000)
AS
begin
declare @temp table(id int identity primary key,pwd char(1))
declare @i int,@sql varchar(1000)
set @i=0
while @i<10
begin
insert into @temp select ltrim(@i)
set @i=@i+1
end
set @i=65
while @i<91
begin
insert into @temp select char(ltrim(@i))
set @i=@i+1
end
set @i=97
while @i<123
begin
insert into @temp select char(ltrim(@i))
set @i=@i+1
end
insert into @temp select '!'
union all select '@'
union all select '#'
union all select '$'
union all select '%'
union all select '^'
union all select '&'
union all select '*'
union all select '('
union all select '_'
union all select '+'
union all select '~'
declare @s varchar(8000)
set @i=0
while @i<@count
begin
select top 1 @s=isnull(@s,'')+pwd from @temp order by (select * from V_getnewid)
set @i=@i+1
end
return @s
end
go
CREATE view V_getnewid
AS
select newid() AS rand_id
go
select dbo.GetPwd(16)
go
--结果:
/**//*
Re~IWVYLe3s%#Tqo
*/
CREATE FUNCTION GetPwd(@count int)
RETURNS varchar(8000)
AS
begin
declare @temp table(id int identity primary key,pwd char(1))
declare @i int,@sql varchar(1000)
set @i=0
while @i<10
begin
insert into @temp select ltrim(@i)
set @i=@i+1
end
set @i=65
while @i<91
begin
insert into @temp select char(ltrim(@i))
set @i=@i+1
end
set @i=97
while @i<123
begin
insert into @temp select char(ltrim(@i))
set @i=@i+1
end
insert into @temp select '!'
union all select '@'
union all select '#'
union all select '$'
union all select '%'
union all select '^'
union all select '&'
union all select '*'
union all select '('
union all select '_'
union all select '+'
union all select '~'
declare @s varchar(8000)
set @i=0
while @i<@count
begin
select top 1 @s=isnull(@s,'')+pwd from @temp order by (select * from V_getnewid)
set @i=@i+1
end
return @s
end
go
CREATE view V_getnewid
AS
select newid() AS rand_id
go
select dbo.GetPwd(16)
go
--结果:
/**//*
Re~IWVYLe3s%#Tqo
*/