Hi,
Try this:
CREATE procedure dbo.GenerateRandomString (
@useNumbers bit,
@useUpperCase bit,
@string varchar(100) OUT
)
As
Begin
DECLARE @charactersToUse VARCHAR(8), @stringLength INT
SET @charactersToUse='ABCDEF'
SET @stringLength=8
if @stringLength <= 0
raiserror('Cannot generate a random string of zero length.',16,1)
declare @characters varchar(100)
declare @count int
set @characters = ''
if @useNumbers = 1
begin
set @count = 48
while @count <=57
begin
set @characters = @characters + Cast(CHAR(@count) as char(1))
set @count = @count + 1
end
end
if @useUpperCase = 1
begin
set @count = 97
while @count <=122
begin
set @characters = @characters + Cast(CHAR(@count) as char(1))
set @count = @count + 1
end
end
set @count = 0
set @string = ''
if Len(@charactersToUse) > 0
begin
while charindex(@charactersToUse,' ') > 0
begin
set @charactersToUse = replace(@charactersToUse,' ','')
end
if Len(@charactersToUse) = 0
raiserror('Cannot use an empty character set.',16,1)
while @count <= @stringLength
begin
set @string = @string +
SUBSTRING(@charactersToUse,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as
int)%LEN(@charactersToUse)+1,1)
set @count = @count + 1
end
end
else
begin
while @count <= @stringLength
begin
set @string = @string +
SUBSTRING(@characters,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as
int)%LEN(@characters)+1,1)
set @count = @count + 1
end
end
end
GO
All the best.
--Amit