sql >> Database >  >> RDS >> Sqlserver

crypt_gen_random gebruiken om unieke serienummers te genereren

Ik heb de code aangepast om unieke serienummers in te voegen, zoals hieronder.

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



  1. Bestanden uploaden en downloaden PHP en MySQL

  2. R- Speciale tekens worden niet in mysql ingevoegd

  3. Waarde splitsen van één veld naar twee

  4. sun.security.validator.ValidatorException:PKIX-padopbouw mislukt, met java>1.6