以下のように一意のシリアル番号を挿入するようにコードを修正しました。
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
プレ>