日期:2014-05-18  浏览次数:20369 次

B表向A表插入多条不重复数据问题,求帮助...
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是自增长问题导致的,求各位大侠帮忙解决下...谢谢了!

------解决方案--------------------
insert into A (code) select code
 from B where code not in(select code from A)
------解决方案--------------------
SQL code

SET IDENTITY_INSERT A ON

------解决方案--------------------
SQL code
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

------解决方案--------------------
你的猜测的 对的,把 * 换成具体的字段(和 A表中的字段对应即可)