-- sql插入数据时提示重复中止
下面是某个表beforesavedatarow的代码,目的时将新增加的“产品编码,工序号及仓库组合” 还不存在库存表的产品加入到库存表中。因为保存前有很多更新的动作全部都是添加到一个sql事务函数中统一执行的。
Dim edr As DataRow = e.DataRow
Dim flt2 As String = "仓库 = \'" & edr("仓库") & "\' and 产品编码 = \'" & edr("产品编码") & "\' and 工序号 = " & edr("工序号")
\'---新增库存表中没有的产品编码
Dim Incol As String = "仓库,产品编码,品名,规格,工序号,工序名称,计量单位"
Dim Inval As String = "\'" & edr("仓库") & "\',\'" & edr("产品编码") & "\',\'" & edr("品名") & "\',\'" & edr("规格") & "\'," & edr("工序号") & ",\'" & edr("加工内容") & "\',\'" & edr("计量单位") & "\'"
str = "If not exists (Select * from 库存表 where " & flt2 & ")" & vbcrlf
str + = "begin " & vbcrlf
str + = "INSERT INTO 库存表 (" & Incol & ") VALUES(" & Inval & ")" & vbcrlf
str + = "End " & vbcrlf
SQLLIST.Add(str) \'将语句添加到事务中
上面已经先用if not exists判断是否存在,不存在再insert.但是新的产品加入时总会提示重复(为避免重复,库存表中用产品编码,仓库,工序号三者建立了组合索引)
此主题相关图片如下:qq图片20141008104617.jpg

为什么会这样提示?
谢谢!