数据库批量添加数据。
从1到1000的编号往数据库里插入,每个编号一条记录,遇到数据库已经存在的编号则跳过,继续执行下边的。存储过程接收两个参数,即‘1’和‘1000’。

解决方案 »

  1.   

    插入语句?insert into 表名 (code) values ("编号")
      

  2.   

    If not exists(select 1 from 表名 where 编号='编号')
    insert into 表名 (code) values ('编号')
      

  3.   

    declare @i int
    set @i=1
    while @i<=1000
    begin
      insert into 表名(code) 
      select @i 
      where not exists(select 1 from 表名 where code=@i)
      set @i=@i+1
    end
      

  4.   


    DECLARE @i int
    DECLARE @j int 
    set @i=1
    set @j=1000
    insert into tb(Id)
    select b.number 
    from master..spt_values b where b.type='P' 
    and b.number>=@i and b.number<=@j
    and NOT Exists(select 1 from tb t where b.number=t.Id)