Home Forums SQL Server 2005 T-SQL (SS2K5) Primary key violation error when I'm not violating the primary key RE: Primary key violation error when I'm not violating the primary key

  • If you're getting that error, you're either trying to insert a row that already exists or there are duplicates in the set you're inserting. There's no other reasons you'd be getting a pk violation.

    Try using a NOT EXISTS instead of the join. Should be equivalent, but a not exists is harder to mes up (eg if the column you're checking for null is nullable itself)

    Check this to see if there are dups in the source data:

    SELECT

    u.SR_ID

    ,u.IR_ID

    ,u.Ordernr

    ,count(*)

    FROM #Usage u

    group by u.SR_ID

    ,u.IR_ID

    ,u.Ordernr

    order by count(*) desc

    What are the data types involved?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass