我有一张处方表,我希望根据患者表中的条件设置非活动状态。这有用吗?
update Prescription
set active = 0
from prescription left join patients on patients.id = prescription.patientid
where patients.[site] = @site
我不想冒险,因为我有一个活跃的生产数据库。
答案 0 :(得分:2)
使用简单的子查询:
UPDATE Prescription
SET active = 0
WHERE patientid IN(SELECT id FROM patients WHERE [site] = @site)
或强>
UPDATE Prescription
SET active = 0
WHERE EXISTS (SELECT id FROM patients p WHERE p.id=Prescription.patientid AND p.[site] = @site)