Hello Friends, In this tutorial, I am going to write a Stored Procedure to Generate Password in SQL Server.

How to Generate Password using Stored Procedure in SQL Server?

Here is the Script to Generate Password using Stored Procedure in SQL Server.

Create Procedure[dbo].[uspGeneratePassword]

@Passwordvarchar(50)OUTPUT

AS

BEGIN

DECLARE@PasswordLengthint;

DECLARE@Countint;

DECLARE@Rndint;

SET@PasswordLength= 10;

SET@Password='';

SET@Count= 1;

WHILE@Count<=@PasswordLength

BEGIN

SET@Rnd=Convert(int,(1 +RAND()*(9-1)));

SELECT@Password=@Password+

CASE

WHEN@Rnd>=1 AND@Rnd<=3 THEN

CHAR(Convert(int,(97 +RAND()*(122-97))))

WHEN@Rnd>=4 AND@Rnd<=6 THEN

CHAR(Convert(int,(65 +RAND()*(90-65))))

WHEN@Rnd= 7 THEN

CHAR(Convert(int,(48 +RAND()*(57-48))))

ELSE

CHAR(Convert(int,(35 +RAND()*(38-35))))

END

SET@Count=@Count+ 1;

END

END;

Run the above script.

To see the output of the Stored Procedure run the below script.


DECLARE@PassNVARCHAR(50)

exec [dbo].[uspGeneratePassword]@Password=@PassOutput

SELECT@Pass

After running the above script we will get the output like this

Generate Password

Conclusion:

I hope this post in SQL Server is useful for you. I would like to have your comments, feedback and suggestions.

Thank You.

Leave a Comment