-- Tables

-- Alters

ALTER TABLE VMP.T_VMP_PLAN ADD "ehrrusfmqd3f8d1m" uuid NULL;

UPDATE VMP.T_VMP_PLAN vp
SET EhrRusFmqd3f8d1m = (select ehh.EhrRusID 
								from EHR.T_EHR_RUS_FMQD3F8D1M ehh
									join EHR.T_EHR_RUS erhh on erhh.EhrRusID = ehh.EhrRusID
								where erhh.IdEHR = Cast(eh.CodeEHR as int) limit 1)
from VMP.T_VMP_PLAN vpp 
join EHR.T_EHR_RUS eh on eh.EhrRusID = vpp.EhrRusOo4khhdi90
where vpp.VmpPlanId = vp.VmpPlanId;

ALTER TABLE VMP.T_VMP_PLAN ALTER COLUMN "ehrrusfmqd3f8d1m" SET NOT NULL;

ALTER TABLE VMP.T_VMP_PLAN ADD CONSTRAINT FK_T_EHR_RUS_FMQD3F8D1M_T_VMP_PLAN_EhrRusFmqd3f8d1m
FOREIGN KEY("ehrrusfmqd3f8d1m") REFERENCES EHR.T_EHR_RUS_FMQD3F8D1M (EhrRusID);




ALTER TABLE VMP.T_VMP_EXECUTION_RESULT_STAGE ADD "ehrrusfmqd3f8d1m" uuid NULL;

UPDATE VMP.T_VMP_EXECUTION_RESULT_STAGE vp
SET EhrRusFmqd3f8d1m = (select ehh.EhrRusID 
								from EHR.T_EHR_RUS_FMQD3F8D1M ehh
									join EHR.T_EHR_RUS erhh on erhh.EhrRusID = ehh.EhrRusID
								where erhh.IdEHR = Cast(eh.CodeEHR as int) limit 1)
from VMP.T_VMP_EXECUTION_RESULT_STAGE vpp 
join EHR.T_EHR_RUS eh on eh.EhrRusID = vpp.EhrRusOo4khhdi90
where vpp.VmpPlanId = vp.VmpPlanId;

ALTER TABLE VMP.T_VMP_EXECUTION_RESULT_STAGE ALTER COLUMN "ehrrusfmqd3f8d1m" SET NOT NULL;

ALTER TABLE VMP.T_VMP_EXECUTION_RESULT_STAGE ADD CONSTRAINT FK_T_EHR_RUS_FMQD3F8D1M_T_VMP_EXECUTION_RESULT_STAGE_EhrRusFmqd3f8d1m
FOREIGN KEY("ehrrusfmqd3f8d1m") REFERENCES EHR.T_EHR_RUS_FMQD3F8D1M (EhrRusID);