Why would this statement produce Primary Key violation error?

Naomi Nosonovsky 8,881 Reputation points
2025-11-09T20:46:33.1966667+00:00

Hi everyone,

Why would this code produce primary key violation error?

DROP TABLE IF EXISTS #temp
SELECT TOP (1000) 

indiv.npi, TRIM(lastname +', '+ firstname + ' ' + LEFT(middlename, 1)) AS fullname, firstname, lastname, 
LEFT(middlename, 1) AS middle_initial,
CURRENT_TIMESTAMP AS create_dtm, CURRENT_TIMESTAMP AS last_update_dtm,  
CURRENT_USER AS last_update_user
INTO #temp
FROM consume_tbl.PROV_PMF_INDIV_PROVIDER indiv
LEFT JOIN dbo.RPT_VA_Mission_Act_Provider miss
ON indiv.npi = miss.npi
WHERE miss.npi IS NULL AND indiv.NPI IS NOT NULL
AND indiv.npi <> '1003001272'
ORDER BY indiv.npi


INSERT INTO dbo.RPT_VA_Mission_Act_Provider
(
    npi,
    provider_fullname,
    provider_first_name,
    provider_last_name,
    provider_middle_initial,
    create_dttm,
    last_update_dttm,
    last_update_user
)
SELECT npi, cte.fullname, cte.firstname, cte.lastname, cte.middle_initial,
cte.create_dtm, cte.last_update_dtm, cte.last_update_user
FROM #temp cte WHERE NOT EXISTS(SELECT 1 FROM dbo.RPT_VA_Mission_Act_Provider 
WHERE cte.npi = RPT_VA_Mission_Act_Provider.npi)


SQL Server | SQL Server Transact-SQL
0 comments No comments
{count} votes

Answer accepted by question author
  1. Q&A Assist
    2025-11-09T20:47:19.64+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The primary key violation error in your SQL statement can occur for several reasons:

    1. Duplicate Values: The INSERT statement attempts to insert rows into the dbo.RPT_VA_Mission_Act_Provider table where the npi column is defined as a primary key. If the npi values being inserted already exist in the dbo.RPT_VA_Mission_Act_Provider table, this will trigger a primary key violation error.
    2. NOT EXISTS Clause: While you have a WHERE NOT EXISTS clause intended to prevent duplicates, it may not be functioning as expected. If there are concurrent transactions inserting into the same table, or if the #temp table contains duplicate npi values, the check may not prevent all duplicates from being inserted.
    3. Data Integrity: Ensure that the data in consume_tbl.PROV_PMF_INDIV_PROVIDER does not contain duplicates for the npi column, especially after applying the filters in your SELECT statement.
    4. Temporary Table: If the #temp table is populated with duplicate npi values before the INSERT operation, it will still attempt to insert those duplicates into the target table, leading to a violation.

    To resolve this issue, you can:

    • Check for duplicates in the #temp table before the INSERT operation.
    • Ensure that the npi values in #temp are unique.
    • Consider using a DISTINCT clause in your SELECT statement to eliminate duplicates from the temporary table.

    By addressing these points, you should be able to prevent the primary key violation error from occurring.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.