ALTER TABLE AKUZ.T_POST_RELATIONSHIP ALTER COLUMN "post" SET NOT NULL;
ALTER TABLE AKUZ.T_POST_RELATIONSHIP ALTER COLUMN "ambulancecardrecord" SET NOT NULL;

ALTER TABLE AKUZ.T_POST_RELATIONSHIP ADD "ehrrusmdp365" integer NULL;

DO $$
DECLARE
    _nurse INT;
    _assistent INT;
BEGIN

    SELECT ehr.EhrRusID INTO _nurse
	    FROM EHR.T_EHR_RUS_MDP365 ehr
    WHERE ehr.IdEHR = 125
    LIMIT 1;

    SELECT ehr.EhrRusID INTO _assistent
        FROM EHR.T_EHR_RUS_MDP365 ehr
    WHERE ehr.IdEHR = 246
    LIMIT 1;

    UPDATE AKUZ.T_POST_RELATIONSHIP pr
	SET EhrRusMdp365 = _nurse
        FROM AKUZ.T_MEDICAL_RECORD mr
    INNER JOIN VCLib.T_ENTITIES e
        ON e.EntityID = mr.type_
    WHERE mr.MedicalRecordID = pr.AmbulanceCardRecord AND e.EntName IN ('MEDICAL_EXAMINATION', 
                                                                        'DOCTOR_WARD_EXAMINATION', 
                                                                        'MEDICAL_EXAMINATION_MAIN', 
                                                                        'MEDICAL_EXAMINATION_RE', 
                                                                        'REANIMATION_EXAMINATION');

    UPDATE AKUZ.T_POST_RELATIONSHIP pr
	SET EhrRusMdp365 = _assistent
        FROM AKUZ.T_MEDICAL_RECORD mr
    INNER JOIN VCLib.T_ENTITIES e
        ON e.EntityID = mr.type_
    WHERE mr.MedicalRecordID = pr.AmbulanceCardRecord AND e.EntName IN ('DIAGNOSTICS_RESULTS', 
                                                                        'ANGIO_DIAGNOSTICS', 
                                                                        'ARMIS_DIAGNOSTICS', 
                                                                        'CARDIO_DIAGNOSTICS', 
                                                                        'DIAGNOSTICS_RESULTS_HISTOLOGICAL', 
                                                                        'ECHOCARDIOGRAPHY_PROTOCOL', 
                                                                        'HEARING_DIAGNOSTICS', 
                                                                        'HEMODIALYSIS_RECORD', 
                                                                        'HITECH_DIAGNOSTICS', 
                                                                        'HOLTER_DIAGNOSTICS', 
                                                                        'INTOXICATION_RESEARCH_RESULT', 
                                                                        'MMG_DIAGNOSTICS', 
                                                                        'PREGNANT_DIAGNOSTICS', 
                                                                        'PRG_DIAGNOSTICS', 
                                                                        'X_RAY_DIAGNOSTICS');      
    
END $$;

ALTER TABLE AKUZ.T_POST_RELATIONSHIP ALTER COLUMN "ehrrusmdp365" SET NOT NULL;

ALTER TABLE AKUZ.T_POST_RELATIONSHIP ADD CONSTRAINT FK_T_EHR_RUS_MDP365_T_POST_RELATIONSHIP_EhrRusMdp365
FOREIGN KEY("ehrrusmdp365") REFERENCES EHR.T_EHR_RUS_MDP365 (EhrRusID);