如题,该怎么办,两张表A,B. 
B表插入A表,A表有iIndex字段,B表没有。
该怎么插?  自己造索引值?真的造了,将来会不会跟A表本身自增长的重复?

解决方案 »

  1.   

    declare @count intset @count = (select count(*) from A)insert into A
    select ROW_NUMBER() over (order by iIndex) + @count , *
    from B
      

  2.   

    如果A表的iIndex是自增长的,那不用管,insert语句里不要有iIndex字段即可。
    如果不是自增长,那么需要考虑这个字段取值的策略,到底放什么值,如果要将A表和B表的值区别,那么凡是从B表来的需要设定一个统一标记。
      

  3.   

    哎,
    无法将NULL值插入iIndex
    B表插入A表,A表有iIndex字段,B表没有
    ----B表加入字段iIndex,并且自动编号
    select   * , identity(int,1,1) as iIndex  
    into #temp from B
    改名B表备份
    truncate table B
    select * into B from #temp
    drop table #temp
    --到这里成功的给B表加入了iIndex加入A表,执行
    declare @count intset @count = (select count(*) from A)insert into A
    select ROW_NUMBER() over (order by iIndex) + @count , *
    from B提示:消息 8101,级别 16,状态 1,第 5 行
    仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'dbo.tDummyRelation'中的标识列指定显式值。
      

  4.   

    A表iIndex字段是自增长的,类型是bigint, B表加入iIndex后,类型是int
    这样插入没关系吧?
    ‘如果A表的iIndex是自增长的,那不用管,insert语句里不要有iIndex字段即可。’
    不要的话 order by  后面跟什么?