zoukankan      html  css  js  c++  java
  • 递增数据插入表(二)

    递增数据插入表,我们还可以使用生成随机数来填充,建表的时候使用约束,不能重复,重复的话自然就不插入,继续生成下一个不重复的随机数。

    还是以之前的为例,插入表中的数据为100-999。我们不谈性能(插入数据量小速度也很快的),只谈逻辑。

    create table TestNum(num numeric(3,0) unique,check (num between 100 and 999));
    go
    declare @num int
    set @num=(select COUNT(*) from TestNum)
    while (@num < 900) 
    begin
    insert into TestNum 
    select round(RAND(CHECKSUM(newid()))*1000,0)
    set @num = (select count(*) from TestNum)
    end;
    go
    select COUNT(*) from TestNum;
    go
    select * from TestNum order by 1;
    go

    上面的插入可能只是某个需求中最基础的实现,比如下面的111000到111999,前面111不变,后面的改变。实际应用可以生成特定号码段的号码

    create table TestNum(num numeric(6,0) unique,check (num between 111000 and 111999));
    go
    declare @num int
    set @num=(select COUNT(*) from TestNum)
    while (@num < 1000) 
    begin
    insert into TestNum 
    select cast('111'+right('000'+convert(nvarchar(6),round(RAND(CHECKSUM(newid()))*1000,0)),3) as numeric(6,0))
    set @num = (select count(*) from TestNum)
    end;
    go
  • 相关阅读:
    python深浅拷贝
    软件开发目录规范
    编码规范
    python进程、线程、协程的介绍及使用
    soket粘包问题及解决方案
    python socket通信
    数据开发_机器学习
    数据开发_开发工具以及工具链
    数据开发_Python读取文件
    数据开发_Java设计模式_IO以及读取资源文件
  • 原文地址:https://www.cnblogs.com/cnmarkao/p/3760581.html
Copyright © 2011-2022 走看看