Generating random strings with T-SQL

前端 未结 27 1857
青春惊慌失措
青春惊慌失措 2020-11-29 18:10

If you wanted to generate a pseudorandom alphanumeric string using T-SQL, how would you do it? How would you exclude characters like dollar signs, dashes, and slashes from

27条回答
  •  一生所求
    2020-11-29 18:44

    This uses rand with a seed like one of the other answers, but it is not necessary to provide a seed on every call. Providing it on the first call is sufficient.

    This is my modified code.

    IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND object_id = OBJECT_ID(N'usp_generateIdentifier'))
    DROP PROCEDURE usp_generateIdentifier
    GO
    
    create procedure usp_generateIdentifier
        @minLen int = 1
        , @maxLen int = 256
        , @seed int output
        , @string varchar(8000) output
    as
    begin
        set nocount on;
        declare @length int;
        declare @alpha varchar(8000)
            , @digit varchar(8000)
            , @specials varchar(8000)
            , @first varchar(8000)
    
        select @alpha = 'qwertyuiopasdfghjklzxcvbnm'
            , @digit = '1234567890'
            , @specials = '_@#$&'
        select @first = @alpha + '_@';
    
        -- Establish our rand seed and store a new seed for next time
        set  @seed = (rand(@seed)*2147483647);
    
        select @length = @minLen + rand() * (@maxLen-@minLen);
        --print @length
    
        declare @dice int;
        select @dice = rand() * len(@first);
        select @string = substring(@first, @dice, 1);
    
        while 0 < @length 
        begin
            select @dice = rand() * 100;
            if (@dice < 10) -- 10% special chars
            begin
                select @dice = rand() * len(@specials)+1;
                select @string = @string + substring(@specials, @dice, 1);
            end
            else if (@dice < 10+10) -- 10% digits
            begin
                select @dice = rand() * len(@digit)+1;
                select @string = @string + substring(@digit, @dice, 1);
            end
            else -- rest 80% alpha
            begin
                select @dice = rand() * len(@alpha)+1;
    
                select @string = @string + substring(@alpha, @dice, 1);
            end
    
            select @length = @length - 1;   
        end
    end
    go
    

提交回复
热议问题