• 生成10位由大小写字母和数字组成的随机激活码


    /*
    select char(65+ceiling(rand()*25))   --随机字母(大写)
    select char(97+ceiling(rand()*25))   --随机字母(小写)
    select cast(ceiling(rand()*9) as varchar(1))   --随机数字 1至9的随机数字(整数)
    */
    
    DECLARE  @i    int           
    DECLARE  @flag int
    DECLARE  @SerialNumber  nvarchar(20)
    
    --初始化设定
    SET  @i=1
    SET  @SerialNumber = ''
    
    --生成10位随机码
    WHILE @i<11
    BEGIN
        --设置随机,这个随机会选择字母(大小写)还是数字
        SET @flag=ceiling(rand()*3) 
    
        IF @flag=1 
        BEGIN
           --随机字母(大写)
           select @SerialNumber=@SerialNumber+char(65+ceiling(rand()*25))
        END
        else if @flag=2
             begin
                 --随机字母(小写)
                 select @SerialNumber=@SerialNumber+char(97+ceiling(rand()*25))
             end
             else begin
                 --随机数字 1至9的随机数字(整数)
                 select @SerialNumber=@SerialNumber+cast(ceiling(rand()*9) as varchar
    
    (1))
             end
    
         --进行下一个循环
         SET @i=@i+1
    END
    
    SELECT @SerialNumber as '生成的10位激活码'

    ---但是有的时候需要前面几位必须是纯英文字母,后面几位纯数字或随意字母和数字

    /*
    select char(65+ceiling(rand()*25))   --随机字母(大写)
    select char(97+ceiling(rand()*25))   --随机字母(小写)
    select cast(ceiling(rand()*9) as varchar(1))   --随机数字 1至9的随机数字(整数)
    */
    
    DECLARE  @i    int           
    DECLARE  @flag INT
    DECLARE  @SerialNumber nvarchar(20)
    
    --初始化设定
    SET  @i=1
    SET  @SerialNumber = ''
    
    --生成10位随机码
    WHILE @i<11
    BEGIN
        --设置随机,这个随机会选择字母(大小写)还是数字
        SET @flag=ceiling(rand()*3) 
    
        IF @i < 6  --前五位是字母
        BEGIN
                IF @flag=1 
                BEGIN
                     --随机字母(大写)
                     select @SerialNumber=@SerialNumber+char(65+ceiling(rand()*25))
                END
                ELSE BEGIN
                     --随机字母(小写)
                     select @SerialNumber=@SerialNumber+char(97+ceiling(rand()*25))
                END
    
         END
         ELSE BEGIN  --后面几位是字母或数字
                IF @flag=1 
                BEGIN
                   --随机字母(大写)
                   select @SerialNumber=@SerialNumber+char(65+ceiling(rand()*25))
                END
                else if @flag=2
                     begin
                         --随机字母(小写)
                         select @SerialNumber=@SerialNumber+char(97+ceiling(rand()*25))
                     end
                     else begin
                         --随机数字 1至9的随机数字(整数)
                         select @SerialNumber=@SerialNumber+cast(ceiling(rand()*9) as varchar(1))
                     end
         END
         
         
         --进行下一个循环
         SET @i=@i+1
    END
    
    SELECT @SerialNumber as '生成的10位激活码'
  • 相关阅读:
    神经网络和Deep Learning
    SQL 优化tips 及误区
    特殊字符的八进制含义
    精通Web Analytics 2.0 (4) 第二章:选择你的网络分析灵魂伴侣的最佳策略
    前端学HTTP之客户端识别和cookie
    前端学HTTP之基本认证
    前端学HTTP之安全HTTP
    前端学HTTP之摘要认证
    前端学HTTP之字符集
    前端学HTTP之实体和编码
  • 原文地址:https://www.cnblogs.com/DBArtist/p/ActivationCode.html
Copyright © 2020-2023  润新知