Solved

Updating Table Column with Random Passwords

Posted on 2008-09-29
8
414 Views
Last Modified: 2010-04-21
I have a stored Procedure that generates random passwords. I have a user table with a password column. Some users have Null or Empty password fields. I would like to write a script that fills these empty or NULL fields with a randomly generated password from the stored procedure
0
Comment
Question by:greatseats
  • 4
  • 2
  • 2
8 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 22601369
Could you change to a user defined function?

If so, then you can call like this:
UPDATE users
SET password = dbo.fn_GetRandomPassword()
WHERE IsNull(password, '') = ''

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22601394
Or if you are doing this in the stored procedure, you can take an approach something like this.
DECLARE @userid int -- whatever your identity column type is
 
WHILE EXISTS (SELECT 1 FROM users WHERE IsNull(password, '') = '')
BEGIN
    -- grab next user to work on
    SELECT TOP 1 @userid = userid FROM users WHERE IsNull(password, '') = ''
    -- use your logic you already have for password here
 
    -- update this user
    UPDATE users
    SET password = @password -- set to password you have already calc
    WHERE userid = @userid
END

Open in new window

0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 22601407
Assuming the SP returns the password as an output parameter, something like this will do it.
Alternatively you coudl load up a table with passwords generated by your SP and then apply them.
 

DECLARE @Password VARCHAR(100)
DECLARE @UserName VARCHAR(100)
 
SELECT @UserName = TOP 1 UserNameColumn FROM YourTable WHERE PasswordColumn IS NULL -- seed the loop
 
WHILE @UserName IS NOT NULL
BEGIN
 
  EXEC p_YourProcedure @Password OUTPUT  -- get the next random password
  
  -- Apply the password (assuming UserNameColumn is unique)
  UPDATE YourTable 
  SET PasswordColumn = @Password
  WHERE UserNameColumn = @UserName
 
  SELECT @UserName = TOP 1 UserNameColumn FROM YourTable WHERE PasswordColumn IS NULL
END

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:greatseats
ID: 22601555
I get the following error when i try to execute this script.

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'TOP'.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'TOP'.

do you know what this is about ?


DECLARE @Password VARCHAR(100)
DECLARE @UserName VARCHAR(100)
 
SELECT @UserName = TOP 1 UserNameColumn FROM YourTable WHERE PasswordColumn IS NULL -- seed the loop
 
WHILE @UserName IS NOT NULL
BEGIN
 
  EXEC p_YourProcedure @Password OUTPUT  -- get the next random password
  
  -- Apply the password (assuming UserNameColumn is unique)
  UPDATE YourTable 
  SET PasswordColumn = @Password
  WHERE UserNameColumn = @UserName
 
  SELECT @UserName = TOP 1 UserNameColumn FROM YourTable WHERE PasswordColumn IS NULL
END

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22601563
It has to be like in my comment.
SELECT TOP 1 @userid = userid FROM users WHERE IsNull(password, '') = ''

The TOP 1 must come before the assignment to variable.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22601589
Here is a blend of the approaches since you liked that one.  Since you said password can be empty or NULL I would also change to my approach of comparing to empty string after changing nulls to '' that way you catch both.
DECLARE @Password VARCHAR(100)
DECLARE @UserName VARCHAR(100)
 
SELECT TOP 1 @UserName = UserNameColumn FROM YourTable WHERE IsNull(PasswordColumn, '') = ''
 
WHILE @UserName IS NOT NULL
BEGIN
   EXEC p_YourProcedure @Password OUTPUT  -- get the next random password
  
  -- Apply the password (assuming UserNameColumn is unique)
  UPDATE YourTable 
  SET PasswordColumn = @Password
  WHERE UserNameColumn = @UserName
 
  -- grab next username
  SELECT TOP 1 @UserName = UserNameColumn FROM YourTable WHERE IsNull(PasswordColumn, '') = ''
END

Open in new window

0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 22602930
Sorry I got the TOP 1 around the wrong way. Its not needed anyway.
0
 

Author Closing Comment

by:greatseats
ID: 31501395
I really like this UDF approach, fast, clean and simple - well I had to learn more about UDF and how to create a udf password generator :-) - fun ! I am still trying to get the other SP approaches to work - running into a few issues - Thanks  very much for the assistance
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtual SQL Server 2014 Standard 35 93
Acces SQL Insert Problem 6 32
MS SQL Server select from Sub Table 14 26
DataType for bit Comparison 11 18
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

830 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