批量插入:
insert into table1(field1,field2)
select field1,field2 from table2 where categoryID = 3
-------------
table1 联系主键是field1,field2,如果table1中已经存在此记录,则插入会报错,问题:如果已存在相同记录,则忽略不插入此记录而不报错,接着插入余下没有冲突的记录,应该怎么做?
数据库环境:SQL 2000
请求各位高手指教.谢谢
给你点参考建议~:
思路 左连接 ,b.field1 is null and b.field2 is null就是table1里不存在这个记录,然后在插入,避免重复插入.
insert into table1(field1,field2)
select a.field1,a.field2 from table2 a left join table1 b
on a.field1=b.field1 and a.field2=b.field2
where categoryID = 3 and b.field1 is null and b.field2 is null
楼上的是正确的,应该加前缀,不然会出错的,另外你的field1,field2主键的权限是什么,不为空,自动增值,有些权限是不能插入数值的!!!呵呵!仅供参考!
insert into table1(field1,field2)
select a.field1,a.field2 from table2 a left join table1 b
on a.field1=b.field1 and a.field2=b.field2
where categoryID = 3 and b.field1 is null or b.field2 is null