我从 Raymond Lewallen 的一些存储过程代码开始,用于密码生成器。我想创建一个触发器,每次插入新行(客户)时都会创建一个唯一的 8 个字符 ID。到目前为止我所拥有的:
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
我如何 (a) 获取一个存储过程并使其成为 SQL Server 2008 中的触发器,以及 (b) 如果我需要测试唯一性,我该怎么做?