Question : Update fields from data in the same table

I have a table called CPT which has various codes.  I have to link this table to cptAgency so I can specify what agency I am working on.  Once I do this I can insert new rows into the CPT table where cptAgency.AgencyID = 3.  After I did this insert I forgot to add a field so now I need to do this but I am having trouble with the query.

Below is my query which I don't think is correct.  

Any suggestions?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
set cptrcid = cc.cptrcid
from cpt c
inner join cpt cc on c.cptcode = cc.cptcode
inner join cptagency ca on c.id = cptid
inner join cptagency caa on cc.id = cptid
where ca.agencyid = 16
and caa.agencyid = 3
Open in New Window Select All

Answer : Update fields from data in the same table


UPDATE c
SET c.cptrcid = ca2.cptrcid --update with value agencyid = 3
FROM cpt c
INNER JOIN cptagency ca ON c.id = ca.cptid
INNER JOIN cptagency ca2 ON c.id = ca2.cptid AND ca,agencyid = 3
WHERE ca.agencyid = 16 --only update 16
Random Solutions  
 
programming4us programming4us