ALTER PROCEDURE [dbo].[spWeb_TransactionProjectUpdate]
@transactionId INT,
@projectId INT
AS
IF NOT EXISTS(SELECT * FROM dbo.com_project_transaction_link WHERE pt_tr_transaction_id = @transactionId AND pt_pj_project_id = @projectId)
BEGIN
INSERT INTO dbo.com_project_transaction_link(pt_tr_transaction_id, pt_pj_project_id) VALUES (@transactionId, @projectId)
END
So when we run this with after an identical record is already in the database, it fails. But we have the IF NOT EXISTS there for this exact reason. Why would it be trying to insert if it finds a record?
If that pair of columns
(pt_tr_transaction_id, pt_pj_project_id)is unique, why not just put a UNIQUE INDEX on these two columns? Combine this with theIGNORE_DUP_KEYoption on that index, and you would have an index thatThis check of yours is prone to failure, since you’re not doing anything to prevent two clients to running at the same time; both could first check to see if that row exists and get back a
false, and then both could proceed to insert those values. The check you have is not safe against concurrent execution.