It appear that SQL Server like most other products Random Function really is not that random. So we have this nice little function to generate a 10 char value. Is there a better way to accomplish what the following does. I am betting there is.
DECLARE @SaltCount INT; SELECT @SaltCount = COUNT(*) FROM tmp_NewLogin; PRINT 'Set Salt values for all records' + CAST(@SaltCount AS VARCHAR(10)) DECLARE @CharPool CHAR(83); DECLARE @Salt VARCHAR(10); SET @CharPool = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!'#$%&()*+,-./:;<=>?@'; SET NOCOUNT ON; updateSaltValue: SET @Salt = '' SELECT @Salt = @Salt + SUBSTRING(@CharPool, number, 1) FROM ( SELECT TOP 10 number FROM MASTER..[spt_values] WHERE TYPE = 'p' AND Number BETWEEN 1 AND 83 ORDER BY NEWID() ) AS t UPDATE TOP(1) [table] SET [Salt] = @Salt WHERE [Salt] IS NULL IF (@@ROWCOUNT > 0) GOTO updateSaltValue SET NOCOUNT OFF; PRINT 'Completed setting salts for all records';
Most programmers make a mistake of reinventing the randomization functionality and end up with something that is not random at all. I’d recommend you to stick with built-in RAND() function. Seed it once then fetch as many values as you need.