MSSQL2005 数据库
A表字段:id、code、time、zt
B表字段:id、code插入语句:insert into A select * from B where code not in(select code from A) 分析时没报错,执行时报错:"仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'code'中的标识列指定显式值。" 这个错误个人猜测应该是表中id是自增长问题导致的,求各位大侠帮忙解决下...谢谢了!

解决方案 »

  1.   

    insert into A (code) select code
     from B where code not in(select code from A)
      

  2.   


    SET IDENTITY_INSERT A ON
      

  3.   

    insert into A(code)
    select code
    from B 
    where not exists
    (
    select 1 from A 
    where A.code = B.code
    )
    --id为自增列,不允许有重复值出现,如果要允许的话,可以打开开关
    set identity_insert table_name on
    insert into A
    (
    id,
    code
    )
    select 
    id,
    code
    from B 
    where not exists
    (
    select 1 from A 
    where A.code = B.code
    )
    set identity_insert table_name off