tsql - Update from subquery SQL Server 2008 -


i have tblpatient county name , state name in table. have table both county name , state name , i'm trying normalize tblpatient, structure enter image description here

as can imagine, different states share county names. deal i'm using query

select patientid, admissiondate, dischargedate, patientstate, patientcounty tblpatient patientstate='al' 

i'd update tblpatient.patientcounty equal tblstatecounties.countycode patientcounty , countyname same.

i haven't had dummy's version of how use rollback yet, looks correct me, yet don't want committed possibly silly error.

update tblpatient set tblpatient.patientcounty=tblstatecountes.countycode  ( select patientid, admissiondate, dischargedate, patientstate, patientcounty tblpatient patientstate='al' ) t inner join on tblstatecounties.countyname=tblpatient.countyname 

the query wrote won't parse (you can check enough) because you're missing clause in sub query.you'd need join on t.tblpatient , you'd need tblpatient in main clause.

you can use query below update table.

update tblpatient  set    tblpatient.patientcounty = tblstatecounties.countycode    tblpatient         inner join tblstatecounties                 on tblstatecounties.countyname = tblpatient.patientcounty   patientstate = 'al'         , tblstatecounties.statecode = '01';  

you'll notice removed subquery , used simple clause. since noted counties can share names need filter tblstatecounties.statecode well

also consider making table mapping of statenames statecode (if don't have already)

demo


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -