update episode
set episode.AmbulanceEvent = movement.EmergencyEvent,
	episode.AmbulanceCard = eventbase.AmbulanceCard
from akuz.T_MEDICAL_EPISODE episode
join akuz.T_MOVEMENTS_DIVISION_CARD mdc
on mdc.MedicalEpisodeID = episode.MedicalEpisodeID
join akuz.T_MOVEMENT movement
on mdc.Movement = movement.MovementID
join akuz.T_EVENT_BASE eventbase
on eventbase.EventID = movement.EmergencyEvent
where episode.AmbulanceEvent is null




IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[AKUZ].[T_DISPANSERISATION_INIT]') AND name = N'UQ_T_DISPANSERISATION_INIT_MedicalExaminationData' and is_unique_constraint <> 1)
DROP INDEX [UQ_T_DISPANSERISATION_INIT_MedicalExaminationData] ON [AKUZ].[T_DISPANSERISATION_INIT]
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[AKUZ].[T_DISPANSERISATION_INIT]') AND name = N'UQ_T_DISPANSERISATION_INIT_MedicalExaminationData' and is_unique_constraint = 1)
ALTER TABLE [AKUZ].[T_DISPANSERISATION_INIT] DROP CONSTRAINT [UQ_T_DISPANSERISATION_INIT_MedicalExaminationData]
IF  EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[AKUZ].[T_DISPANSERISATION_INIT]') AND name = N'MedicalExaminationData_UQ')
ALTER TABLE [AKUZ].[T_DISPANSERISATION_INIT] DROP COLUMN [MedicalExaminationData_UQ]
GO