2022-07-12 12:13:15 版本 : SQL_Server 密码生成函数 - czperfectaction - 博客园
作者: 周杨 于 2022年07月12日 发布在分类 / 计算机应用 / 数据库 / SQLSERVER 下,并于 2022年07月12日 编辑
 历史版本

备注 修改日期 修改人
CREAT 2022-07-12 12:13:15[当前版本] 系统管理员



说明:生成一个自定义位数的密码,密码可能由以下元素随机组合而成

一:0,1,2,3,4,5,6,7,8,9

二:a到z(小写)

三:A到Z(大写)

四:!,@,#,$,^,&,*,(,),_,+

使用方法:select(print) dbo.getpwd(密码位数)


代码:

CREATE FUNCTION fn_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 vi_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 )
结果:
Re~IWVYLe3s%#Tqo



历史版本-目录  [回到顶端]
    知识分享平台 -V 5.1.4 -大信谛威