cnmarkao

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

还是以之前的为例,插入表中的数据为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

分类:

技术点:

相关文章:

  • 2021-11-17
  • 2022-02-25
  • 2022-12-23
  • 2022-02-01
  • 2021-05-31
  • 2021-07-08
  • 2021-11-21
猜你喜欢
  • 2021-11-17
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2021-10-17
  • 2022-12-23
相关资源
相似解决方案