使用 crypt_gen_random 生成唯一的序列号
Using crypt_gen_random to generate unique Serial Numbers
尝试使用字母数字(A-Z、a-z、0-9)生成唯一序列号,并且没有特殊的 characters.Used 下面的代码,其中 sLength 是前端定义的最小 10 和最大 12。
declare @sLength tinyint
declare @randomString varchar(50)
declare @counter tinyint
declare @nextChar char(1)
declare @rnd as float
set @sLength = 3
set @counter = 1
set @randomString = ''
while @counter <= @sLength
begin
-- crypt_gen_random produces a random number. We need a random
-- float.
select @rnd = cast(cast(cast(crypt_gen_random(2) AS int) AS float) /
65535 as float)
select @nextChar = char(48 + convert(int, (122-48+1) * @rnd))
if ascii(@nextChar) not in (58,59,60,61,62,63,64,91,92,93,94,95,96)
begin
select @randomString = @randomString + @nextChar
set @counter = @counter + 1
end
end
select @randomString
现在要求已更改,我们将发送在前端选择的一组字符(最少 6 个)和数字(最少 2 个)。所以我将按以下方式发送参数@Include = 'ABCDEFG12345' 和@Exclude='HIJKLMNOPQRSTUVXYZ06789'。有人可以建议我如何根据要求更改以下代码。
我的想法是把if ascii(@nextChar) not in这一行改成把@Exclude字符&数字的所有Ascii码都加进去,但是写不出来相同的代码。
我已修改代码以插入唯一序列号,如下所示。
declare @i int = 4000
while @i>0
begin
declare @sLength tinyint
declare @randomString varchar(50)
declare @counter tinyint
declare @nextChar char(1)
declare @rnd as float
declare @ExcludeNumbers varchar(50)
DECLARE @XML XML
set @ExcludeNumbers='A,B,C,D,E,F,G,H,1,2,3'
SET @XML = CAST('<i>' + REPLACE(@ExcludeNumbers, ',', '</i><i>') + '</i>' AS XML)
set @sLength = 10
set @counter = 1
set @randomString = ''
while @counter <= @sLength
begin
-- crypt_gen_random produces a random number. We need a random
-- float.
select @rnd = cast(cast(cast(crypt_gen_random(2) AS int) AS float) /
65535 as float)
select @nextChar = char(48 + convert(int, (122-48+1) * @rnd))
if ascii(@nextChar) in (select ASCII((x.i.value('.', 'VARCHAR(MAX)'))) FROM @XML.nodes('i') x(i))
begin
select @randomString = @randomString + @nextChar
set @counter = @counter + 1
end
end
insert into serialNo values( @randomString);
select @i = @i-1
End
尝试使用字母数字(A-Z、a-z、0-9)生成唯一序列号,并且没有特殊的 characters.Used 下面的代码,其中 sLength 是前端定义的最小 10 和最大 12。
declare @sLength tinyint
declare @randomString varchar(50)
declare @counter tinyint
declare @nextChar char(1)
declare @rnd as float
set @sLength = 3
set @counter = 1
set @randomString = ''
while @counter <= @sLength
begin
-- crypt_gen_random produces a random number. We need a random
-- float.
select @rnd = cast(cast(cast(crypt_gen_random(2) AS int) AS float) /
65535 as float)
select @nextChar = char(48 + convert(int, (122-48+1) * @rnd))
if ascii(@nextChar) not in (58,59,60,61,62,63,64,91,92,93,94,95,96)
begin
select @randomString = @randomString + @nextChar
set @counter = @counter + 1
end
end
select @randomString
现在要求已更改,我们将发送在前端选择的一组字符(最少 6 个)和数字(最少 2 个)。所以我将按以下方式发送参数@Include = 'ABCDEFG12345' 和@Exclude='HIJKLMNOPQRSTUVXYZ06789'。有人可以建议我如何根据要求更改以下代码。
我的想法是把if ascii(@nextChar) not in这一行改成把@Exclude字符&数字的所有Ascii码都加进去,但是写不出来相同的代码。
我已修改代码以插入唯一序列号,如下所示。
declare @i int = 4000
while @i>0
begin
declare @sLength tinyint
declare @randomString varchar(50)
declare @counter tinyint
declare @nextChar char(1)
declare @rnd as float
declare @ExcludeNumbers varchar(50)
DECLARE @XML XML
set @ExcludeNumbers='A,B,C,D,E,F,G,H,1,2,3'
SET @XML = CAST('<i>' + REPLACE(@ExcludeNumbers, ',', '</i><i>') + '</i>' AS XML)
set @sLength = 10
set @counter = 1
set @randomString = ''
while @counter <= @sLength
begin
-- crypt_gen_random produces a random number. We need a random
-- float.
select @rnd = cast(cast(cast(crypt_gen_random(2) AS int) AS float) /
65535 as float)
select @nextChar = char(48 + convert(int, (122-48+1) * @rnd))
if ascii(@nextChar) in (select ASCII((x.i.value('.', 'VARCHAR(MAX)'))) FROM @XML.nodes('i') x(i))
begin
select @randomString = @randomString + @nextChar
set @counter = @counter + 1
end
end
insert into serialNo values( @randomString);
select @i = @i-1
End