SQL Server Stored Procedure for Generating Random Passwords

Here is an example how to make random password generator using stored procedure in SQL Server.

Use this code to create a stored procedure for generating complex random passwords of desired length (@pass_len), from the list of valid password characters (@ValidChar):

CREATE PROCEDURE usp_RandomPassword (@pass_len AS int, @password nvarchar(400) OUTPUT)
AS
DECLARE @ValidChar AS nvarchar(400)
SET @ValidChar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890~!@#$%^&*()_-+={}[]\|:;"<>,.?/'
DECLARE @counter int
SET @counter = 0
SET @password = ''
WHILE @counter < @pass_len BEGIN SELECT @password = @password + SUBSTRING(@ValidChar, (CONVERT(int, (LEN(@ValidChar) * RAND() + 1))), 1) SET @counter += 1 END; GO

To use the stored procedure to get a 10 characters long password:

DECLARE @new_password varchar(50)
EXEC dbo.usp_RandomPassword @pass_len = 10, @password = @new_password out
SELECT @new_password

Here is a sample output:

10_characters_password_sp

To use the stored procedure to get a 12 characters long password:

DECLARE @new_password varchar(50)
EXEC dbo.usp_RandomPassword @pass_len = 12, @password = @new_password out
SELECT @new_password

Here is a sample output:

12_characters_password_sp

Read related blog post:
SQL Server User Defined Function (UDF) for Generating Complex Random Passwords

You can use our online Password Generator to generate complex random passwords:
Password Generator

If the post helped you, please share it:
Pin It

1 comment to SQL Server Stored Procedure for Generating Random Passwords

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">