insert into b(aa,bb) select aa,bb from a

解决方案 »

  1.   

    给分吧
    declare @i int
    set @i=0
    insert into b(xx,aa,bb) select @i=@i+1,aa,bb from a
      

  2.   

    XX的编号是自动生成的吗?
    如果是
    insert into b (aa,bb)
    select aa,bb from a
      

  3.   

    select identity(int,1,1) as xx,aa,bb into #t from a
    insert into b (xx,aa,bb) select xx,aa,bb from #t
    drop table #t
      

  4.   

    select identity(int,1,1) as xx,aa,bb into #t from a
    insert into b (xx,aa,bb) select xx,aa,bb from #t
    drop table #t
      

  5.   

    在b中的xx上写入每条记录对应的编号?
    不知道你的编号有什么规律还是从别的表关联得到,如果是关联得到,可以用update
    update b
    set xx=..
    from b,其他表
    where ....
      

  6.   

    如果xx不是自动生成的
    declare @i int
    insert into b(xx,bb,aa) select @i=@i+1,bb,aa from a
      

  7.   

    如果编号是int那就用我3楼的方法,如果编号为字符型,就改一下
    select identity(int,1,1) as xx,aa,bb into #t from a
    insert into b (xx,aa,bb) select cast(xx as varchar),aa,bb from #t
    drop table #t
      

  8.   

    wanyingsong:
             
            如果#t的编号和b中的编号重复怎么办?
      

  9.   

    --b表的xx没有设置主键/唯一约束吧?--先插入数据
    insert b(xx,aa,bb) select 0,aa,bb from a--再生成xx
    declare @i int
    set @i=0
    update b set @i=@i+1,xx=@i
      

  10.   

    --如果b表已经有数据,现在的编号要从最大的编号+1开始编,则用:--先插入数据
    insert b(xx,aa,bb) select 0,aa,bb from a--再生成xx
    declare @i int
    selectt @i=max(xx0 from b
    update b set @i=@i+1,xx=@i
    where xx=0
      

  11.   

    to:zilang(随意的风) 
    我理解的楼主的意思是b表没有数据,如果有数据,楼主应该指明
      

  12.   

    --如果b表的xx是主键/唯一约束,则只有用临时表了select id=identity(int),aa,bb into #t from ainsert bb(xx,aa,bb) select a.id+b.id,a.aa,a.bb
    from #t a,(select id=max(xx) from b)bdrop table #t
      

  13.   

    --如果b表已经有数据,现在的编号要从最大的编号+1开始编,则用:--先插入数据
    insert b(xx,aa,bb) select 0,aa,bb from a--再生成xx
    declare @i int
    selectt @i=max(xx) from b  --这里原来写错了.
    update b set @i=@i+1,xx=@i
    where xx=0