Use following script to generate random Passwords
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
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.
You can also create a function and can use any where.
To create function to generate random password, first you need to create a view as
CREATE VIEW getNewID as SELECT newid() as new_id
Because NEWID() does not work in function.Error occurred as
Msg 443, Level 16, State 1, Procedure fn_random, Line 7
Invalid use of a side-effecting operator 'newid' within a function.
After
creating view,You create function like
CREATE function
fn_random()
RETURNS varchar(16)
with encryption
begin
declare @pass varchar(16)
declare @n varchar(max)
select @n=new_id
from getNewID
Select @pass=replace(SUBSTRING(CONVERT(varchar(255),CAST(@n as varchar(max)) ),8, 8),'-','')
return @pass
end
select dbo.fn_random()
Output:
A56B53349
again execute the same
select dbo.fn_random()
Output:
0E87A21C4
Hеllo there, I do think your blog could be havіng web browser compatibility pгoblems.
ReplyDeleteWhеnever I look at your wеbsite іn Safaгi, it looks fine hοwever, if
opening in Іntеrnеt Eхplorer, it has some overlаpρing іssueѕ.
I merely ωanted to give you a quick hеads uρ!
Оther than that, fantastiс blоg!
Also visit my weblog - rv rentals in ohio motor home
GREAT ARTICLE!, DUDE I NEED ONLY NUMERIC VALUE
DeleteLIKE
993223
552343
233434
HOW CAN I ACHIEVE THAT?
you can use
Deletedeclare @number int
set @number = (select cast((rand()*1000000) as decimal(6)))
if len(@number) = 6 --This condition is due to it returns sometimes 5 digidts output.
select @number
else
set @number = convert(varchar,(select cast((rand()*10) as decimal(1))))+convert(varchar,@number)
Thnx tat was vey useful............
ReplyDeleteCan u tel u how to update a table with the value returned by that function?
Hi Raja,
DeleteYou can use below query to update column of a table.
update emp set password=(select dbo.fn_random()) where ID=3
Thnx tat was very useful.........
ReplyDeleteCan u tell me how to update a table with the value returned by that function?
Although this method creates values formed of numeric and characters in general a password has to include special characters too. Thank you for the SQL trick you shared with us. An alternative method is shared at here SQL password generator to create random passwords for the SQL developers to generate random passwords using SQL with special characters.
ReplyDelete