I am using SQL 2005.Currently If I try and do the insert part of the code I am getting a primary key violation. I am trying to write a query to insert unique records into an existing table. Before I do this I am using select statements to verify the number of records is correct before I do the insert. Currently I am stumped on what to do. The dbo.tmp_rpt_dic_CPT table has 611 records and currently the dbo.rpt_dic_CPT table had 478 records in it. I am expecting that if my select query is done correctly my query would return 133 records. It is currently returning 390 records. clntid,cptcode and cptcomp are primary keys. If I got the correct number I would turn the query into a delete query so I could then delete the dups from the dbo.tmp_rpt_dic_CPT table and insert the unique records into the rptdata_monthly.dbo.rpt_dic_CPT table. Any help would be appreciated.
Tom
Tom
Code:
INSERT INTO rptdata_monthly.dbo.rpt_dic_CPT
SELECT DISTINCT CPT1.clntid,CPT1.cptcode,CPT1.cptcomp,CPT1.cptdesc,CPT1.cptrpt
FROM rptdata_monthly.dbo.rpt_dic_CPT CPT1
JOIN dbo.tmp_rpt_dic_CPT CPT2 ON CPT1.clntid = CPT2.clntid and CPT1.cptcode = CPT2.cptcode and CPT1.cptcomp = CPT2.cptcomp
WHERE CPT1.clntid= 38
GROUP BY CPT1.clntid,CPT1.cptcode,CPT1.cptcomp,CPT1.cptdesc,CPT1.cptrpt
ORDER BY CPT1.clntid,cptcode,CPT1.cptcomp,CPT1.cptdesc,CPT1.cptrpt