我在使用此脚本的存储过程中遇到了真正的问题:
INSERT INTO #tr_TxnDetails
SELECT
b.pid,
b.etc
FROM tbl_SomeTableA as a
JOIN tbl_SomeTableB as b ON a.etc = b.etc
AND a.SomeColumn = b.SomeColumn
-- This is throwing error: Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object 'dbo.tr_TxnDetails'.
INSERT INTO tr_TxnDetails
([id], [etc])
SELECT a.[id],
a.[etc]
FROM #tr_TxnDetails as a
WHERE not exists (select 1 from tr_TxnDetails as b where a.[id] = b.[id]);
如何确保在对它的INSERT INTO
语句期间tr_TxnDetails
没有插入具有相同主键的行:pid
?