Neil Toulouse
Programmer
Hi!
A colleague of mine passed me some code he got off the interweb for generating a simple 8 character (random?) code that he wants to implement as part of a very basic password routine.
I tidied up what he sent but I can't get it to work either. The original issue is with the NewID() function which cannot be used in a function. I have messed with RAND() and Row_Number and such like, but always end up with the string '01234567'!!
I am guessing this should be fairly easy, but a lot of the examples on the net seem to be quite complex.
Can anyone shed any light on this?
The 'SELECT TOP 8....' statement works perfectly on its own, just not in a function.
TIA
Neil
I like work. It fascinates me. I can sit and look at it for hours...
A colleague of mine passed me some code he got off the interweb for generating a simple 8 character (random?) code that he wants to implement as part of a very basic password routine.
I tidied up what he sent but I can't get it to work either. The original issue is with the NewID() function which cannot be used in a function. I have messed with RAND() and Row_Number and such like, but always end up with the string '01234567'!!
I am guessing this should be fairly easy, but a lot of the examples on the net seem to be quite complex.
Can anyone shed any light on this?
Code:
CREATE FUNCTION [dbo].[CreatePassword]()
RETURNS varchar(8)
BEGIN
DECLARE @password varchar(8)
SET @password=''
SELECT @password = @password + char(n)
FROM
(
select top 8 number as n from master..spt_values
where type='p' and number between 48 and 122
order by newid()
) AS t
RETURN @password
END
The 'SELECT TOP 8....' statement works perfectly on its own, just not in a function.
TIA
Neil
I like work. It fascinates me. I can sit and look at it for hours...