譬如一张两个字段的表:第一个字段是编号,第二个是名称。现在假如希望插入1000条数据,编号为数字不能重复(例如1——1000),名称可以重复,例如从“张三,李四”选择插入。
请问应该怎样写sql语句?谢谢!

解决方案 »

  1.   

    create table test(a int identity(1,1),name varchar(20))insert into test(name) values('张三')
      

  2.   

    做一自增列字段,俗称种子字段:
    if not object_id('tb1') is null
        drop table tb1
    Go
    Create table tb1([id] int identity,[numb] int)
    Insert tb1
    select 20 union all
    select 30 union all
    select 50 select * from tb1 
    /*
    id          numb
    ----------- -----------
    1           20
    2           30
    3           50(3 行受影响)
    */
    drop table tb1
      

  3.   

    create table t(id int identity(1,1),name varchar(100))declare @s varchar(1000)
    set @s='张三,李四,jinjazz'
    insert into t(name)
    select replace(reverse((left(s,charindex(',',s)))),',','') as S from(
    select r,reverse(left(@s,r))+',' as s
    from(
    select (select count(*) from sysobjects where name<=t.name ) as r
    from sysobjects t
    )a where r<=len(@s)
    and left(@s+',',r+1) like '%,'
    )t order by rselect * from t
    drop table t/*
    (3 行受影响)
    id          name
    ----------- ----------------------------------------------------------------------------------------------------
    1           张三
    2           李四
    3           jinjazz(3 行受影响)
    */