SQL random AND unique password generation?

Posted on 2013-11-08
Last Modified: 2013-11-08
I have found many examples of SQL random password generators but nothing that will check whether it is unique.

I am generating a username/password for security on a public system. The username is an email and the initial password needs to be randomly generated and not equal to any existing password held on my user security table.

I am pretty sure that some one must have a function already?

Any help would be greatly appreciated.

Question by:splanton
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 32

Accepted Solution

Ephraim Wangoya earned 500 total points
ID: 39633152
You can use a loop to check if the password exists in the table
Suppose your function to generate password is ufn_GeneratePassword

You could create a SP that checks the password

create sp_GetUniquePassword(out @password varchar(max))
   declare @temp varchar(max) = ''
   while Exists(select 1 from UserSecuritytable where PasswordField = @temp)
     select @temp = dbo.ufn_GeneratePassword
   set @password = @temp

Expert Comment

ID: 39633294
If you generated something based on the current full date time down to the millisecond, that is almost certain to be unique...

If you need some help with code to to that, give us a shout



Author Closing Comment

ID: 39633331
That's the ticket. I was thinking of creating a second while loop but had no idea on the best way to go about it. Many thanks.


Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question