I started with some stored procedure code by Raymond Lewallen for a password generator. I would like to create a trigger that will create a unique 8 character ID each time a new row (a customer) is inserted. What I have so far:
CREATE procedure dbo.AllAccessIDgenerator (
@showID varchar(40)
@accessID varchar(100) OUT
)
As
Begin
declare @codeLength int
declare @characters varchar(100)
declare @count int
set @characters = ''
set @codeLength = 8
-- set A - Z (uppercase)
set @count = 65
while @count <=90
begin
set @characters = @characters + Cast(CHAR(@count) as char(1))
set @count = @count + 1
end
end
-- set 0-9
set @count = 48
while @count <=57
begin
set @characters = @characters + Cast(CHAR(@count) as char(1))
set @count = @count + 1
end
end
set @count = 0
set @accessID = ''
while @count <= @codeLength
begin
set @accessID = @accessID + SUBSTRING(@characters,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@characters)+1,1)
set @count = @count + 1
end
end
end
GO
How do I (a) Take a stored procedure and make it a trigger in SQL Server 2008 and (b) If I needed to test for uniqueness, how would I do that?