drop procedure if exists public.sp_UpdateReToDweType;
-- =============================================
-- Author:  Michael Panin
-- Create date: 2023/08/08
-- Description: Смена типа DOCTOR_WARD_EXAMINATION на MEDICAL_EXAMINATION_RE
-- =============================================
CREATE procedure public.sp_UpdateReToDweType
(
	"reId" UUID    -- Идентификатор нового DOCTOR_WARD_EXAMINATION
,	"eventId" UUID -- Идентификатор AMBULANCE_EVENT
)

AS
$$ 
BEGIN

    UPDATE AKUZ.T_MEDICAL_RECORD 
	SET type_ = (SELECT EntityID
                 FROM   VCLib.T_ENTITIES
                 WHERE  EntName = 'DOCTOR_WARD_EXAMINATION' AND Schema = 'AKUZ')
	WHERE MedicalRecordID = "reId";

    DELETE FROM AKUZ.T_MEDICAL_EXAMINATION_RE
    WHERE MedicalRecordID = "reId";

    INSERT INTO AKUZ.T_DOCTOR_WARD_EXAMINATION (MedicalRecordID, IsPrint, EmergencyEvent)
    VALUES("reId", 0, "eventId");

END;
$$ 
LANGUAGE plpgsql;







