Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Password Generator UDF

Author  Topic 

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2005-01-14 : 23:54:15
A udf to generate passwords of given length @lenth
--create a tally table for use in the view

create table dbo.numbers ( n int primary key)
declare @n int; set @n = 1
while @n <=1000
begin
insert dbo.numbers
select @n
set @n = @n+1
end


--create a view to expose NEWID() for randomness

create view dbo.vwRandomCharGenerator
as
select top 1000 char(n) n
from dbo.numbers
where n between 65 and 90 or n between 97 and 122 --only A-Z or a-z characters
order by newid()
go


--create password generator function which accepts length as input

create function dbo.udfPassWordGenerator (@length int)
returns varchar(1000)
AS
begin
declare @pwd char(1000)
set @pwd = ''

Select @pwd = n + @pwd
from
(
select n
from dbo.vwRandomCharGenerator

) d
return left(@pwd,@length)
end
go


--call our password generator inline, passing in the desired length

select dbo.udfPassWordGenerator(10) pwd
   

- Advertisement -