有2个数据库A和B,我现在已把A数据库中的一张表的某些字段值插入了B数据库的某张表,但是B数据库的表有个字段REC(是系统记录号唯一的)在A数据库的表是没有的,导致插入后数据错误。如何解决这个问题啊??或者如何update让REC自动从1开始排

解决方案 »

  1.   


    --如果你的rec字段可以从1开始从新设计的话可以
    alter table tb drop column rec
    alter table tb add rec int identity(1,1)  --也可以直接更改为自增列,不用删除!insert into a.dbo.tb(col1,col2,col3)
    select col1,col2,col3 from b.dbo.tb
      

  2.   


    该列已经是自增列,但插入的时候哟是不选择这个列会提示出错,提示该列不能为空。
    用insert语句也是一样不会自增 这是为什么?跨数据库的原因?如果不跨数据库,直接insert他会自增
      

  3.   

    --楼主看下面例子
    use CSDN
    GOcreate table tb_identity
    (
    id int identity(1, 1),
    col1 int,
    col2 int
    )
    GOuse TEST
    GOcreate table tb_no_identity
    (
    col1 int,
    col2 int
    )
    GO
    insert tb_no_identity
    select 1, 2
    GO--SQL
    INSERT CSDN.dbo.tb_identity(col2)
    SELECT col2 FROM TEST.dbo.tb_no_identity--test and result
    SELECT * FROM CSDN.dbo.tb_identity
    /*
    id col1 col2
    1 NULL 2
    */
      

  4.   

    是这样:表:
    ID          rec     parrec
    00001        1        0
    0000100001   2        1
    0000100002   3        1
    0000100003   4        1
    0000100004   5        1
    0000100005   6        1
    类似下去现在问题是:要修改parrec = 1 要怎么修改? update a set parrec = (如何填写,不能写1)
      

  5.   


     update a set parrec =rec-1