zoukankan      html  css  js  c++  java
  • 生成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位激活码'
  • 相关阅读:
    code light
    asp.net(C#)定时自动发送邮件
    示例代码(一)
    devExpress 7.2.5
    使用VSIUAL C#.NET操作Excel -把DataTable中的数据写入Excel
    C#日期格式化的几种处理方法
    配置
    关于Windows 2008 R2 Web服务器环境搭建、安全流程
    安全
    FTP 用户目录病隔离
  • 原文地址:https://www.cnblogs.com/DBArtist/p/ActivationCode.html
Copyright © 2011-2022 走看看