首页 新闻 赞助 找找看

批量插入,数据库记录冲突问题

0
悬赏园豆:5 [已关闭问题]

批量插入:
insert into table1(field1,field2)
select field1,field2 from table2 where categoryID = 3
-------------
table1 联系主键是field1,field2,如果table1中已经存在此记录,则插入会报错,问题:如果已存在相同记录,则忽略不插入此记录而不报错,接着插入余下没有冲突的记录,应该怎么做?

数据库环境:SQL 2000

请求各位高手指教.谢谢

yangjun的主页 yangjun | 初学一级 | 园豆:17
提问于:2009-05-19 12:47
< >
分享
其他回答(2)
0

给你点参考建议~:
思路 左连接 ,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

Frank Xu Lei | 园豆:1860 (小虾三级) | 2009-05-19 13:29
0

楼上的是正确的,应该加前缀,不然会出错的,另外你的field1,field2主键的权限是什么,不为空,自动增值,有些权限是不能插入数值的!!!呵呵!仅供参考!

子夜星辰 | 园豆:1613 (小虾三级) | 2009-05-19 13:34
0

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

hlake | 园豆:225 (菜鸟二级) | 2009-05-19 14:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册