Blog Post

Generate Random Passwords using T-SQL

,


Use following script to generate random Passwords

DECLARE @id int,

@list varchar

SET @list = 'abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789.,-_!$@#%^&*'

select replace(SUBSTRING(CONVERT(varchar(255), NEWID()),8, 8),'-','') as RANDOM



Example:

Suppose you have a table tblUser with columns

[id],[userid],[password].

CREATE TABLE [dbo].[tblUser](

      [id] [int] IDENTITY(1,1) NOT NULL,

      [userid] [varchar](250) NULL,

      [password] [varchar](350) NULL,

      )


Now Create a trigger on table tblUser and update password column with random password

CREATE TRIGGER [dbo].[insUser]

   ON  [dbo].[tblUser]

   AFTER Insert

AS

BEGIN

      -- SET NOCOUNT ON added to prevent

extra result sets from

      -- interfering with SELECT

statements.

      SET NOCOUNT

ON;

  DECLARE @id int,

  @list varchar
SET @list = 'abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789.,-_!$@#%^&*'

SET @id = (SELECT max(id) FROM tblUser)

UPDATE tblUser set password =replace(SUBSTRING(CONVERT(varchar(255), NEWID()),8, 8),'-','') WHERE id=@id

END


this trigger create random password.

     

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating