主键违规约束
string sqlInsert = "Insert into account_details(
account_number,
account_type,
transfer_access_code,
account_balance,
customer_id)
SELECT
account_number,
account_type,
transfer_access_code,
account_balance,
customer_id
from customer_details";
此查询仅从客户详细信息(table1)中获取数据,并将其插入到其他表2(account_details)中
,当第一次触发此查询时,它工作正常,
但第二次触发时,它显示错误 违反主键约束“PK_account_details”。无法在对象“dbo.account_details”中插入重复的键。
任何想法可以跳过 (account_details)table1 中的现有数据并在下一行中插入新数据
string sqlInsert = "Insert into account_details(
account_number,
account_type,
transfer_access_code,
account_balance,
customer_id)
SELECT
account_number,
account_type,
transfer_access_code,
account_balance,
customer_id
from customer_details";
This query just takes data from customer details(table1) and insert's it in other table2(account_details)
when this query is fired for first time it works fine
but when fired second time it show error
Violation of PRIMARY KEY constraint 'PK_account_details'. Cannot insert duplicate key in object 'dbo.account_details'.
any idea to skip existing data in (account_details)table1 and inserting new data in next row
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
LEFT JOIN
允许您排除帐户详细信息
表中已存在的所有行。替代方案(可能甚至更快)是使用NOT EXISTS
。使用
LEFT JOIN
使用
NOT EXISTS
Using a
LEFT JOIN
allows you to exclude all the rows that are already present in theaccount details
table. An alternative to this (and probably even faster) is using aNOT EXISTS
.using a
LEFT JOIN
using a
NOT EXISTS
假设 Account_Number 是主键,这应该可以工作
assuming Account_Number is the primary key, this should work