IF  EXISTS (SELECT * 
			FROM   dbo.sysobjects 
			WHERE  id = OBJECT_ID(N'[dbo].[sp_UpdateReToDweType]') AND xtype = N'P')
DROP PROCEDURE [dbo].[sp_UpdateReToDweType]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:  Michael Panin
-- Create date: 2023/08/08
-- Description: Смена типа DOCTOR_WARD_EXAMINATION на MEDICAL_EXAMINATION_RE
-- =============================================

CREATE PROCEDURE [dbo].[sp_UpdateReToDweType]
	@reId uniqueidentifier,    -- Идентификатор DOCTOR_WARD_EXAMINATION
	@eventId uniqueidentifier  -- Идентификатор AMBULANCE_EVENT
AS 
BEGIN

	UPDATE [AKUZ].T_MEDICAL_RECORD
	SET    [AKUZ].T_MEDICAL_RECORD.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
GO
