IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_DispDirectFactDel]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_DispDirectFactDel]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[rds].[sp_DispDirectFactDel]') AND type in (N'P', N'PC'))
DROP PROCEDURE [rds].[sp_DispDirectFactDel]
GO


-- =============================================
-- Description: Загрузка фактов прохождения диспансеризации
-- =============================================
CREATE PROCEDURE [rds].[sp_DispDirectFactDel]
  @FileID uniqueidentifier
AS 
BEGIN 

 
DELETE d FROM [RDS].T_DISP_DIRECT_FACT_DESTINATION d
join [RDS].T_DISP_DIRECT_FACT f on d.DispDirectFact = f.DispDirectFactID
WHERE f.[DispDirectFactFile] = @FileID

DELETE FROM [RDS].T_DISP_DIRECT_FACT
WHERE [DispDirectFactFile] = @FileID

DELETE s FROM [RDS].T_DISP_DIRECT_STATUS s
	join [RDS].T_DISP_DIRECT d on s.DispDirect = d.DispDirectID
	left join [RDS].T_DISP_DIRECT_FACT ddf on d.DispDirectID = ddf.DispDirect
WHERE d.[DispDirectFactFile] = @FileID and ddf.DispDirectFactID is null

DELETE i FROM [RDS].T_DISP_DIRECT_INTERVIEW i
	join [RDS].T_DISP_DIRECT d on i.DispDirect = d.DispDirectID
	left join [RDS].T_DISP_DIRECT_FACT ddf on d.DispDirectID = ddf.DispDirect
WHERE d.[DispDirectFactFile] = @FileID and ddf.DispDirectFactID is null

DELETE i FROM [RDS].T_DISP_DIRECT_INFO i
	join [RDS].T_DISP_DIRECT d on i.DispDirect = d.DispDirectID
	left join [RDS].T_DISP_DIRECT_FACT ddf on d.DispDirectID = ddf.DispDirect
WHERE d.[DispDirectFactFile] = @FileID and ddf.DispDirectFactID is null



DELETE a1 FROM [RDS].T_DISP_DIRECT_ANKETA1 a1
	join [RDS].T_DISP_DIRECT_QUESTIONING q on a1.DiapDirectQuestioningID = q.DiapDirectQuestioningID
	join [RDS].T_DISP_DIRECT d on q.DispDirect = d.DispDirectID
	left join [RDS].T_DISP_DIRECT_FACT ddf on d.DispDirectID = ddf.DispDirect
WHERE d.[DispDirectFactFile] = @FileID and ddf.DispDirectFactID is null

DELETE a2 FROM [RDS].T_DISP_DIRECT_ANKETA2 a2
	join [RDS].T_DISP_DIRECT_QUESTIONING q on a2.DiapDirectQuestioningID = q.DiapDirectQuestioningID
	join [RDS].T_DISP_DIRECT d on q.DispDirect = d.DispDirectID
	left join [RDS].T_DISP_DIRECT_FACT ddf on d.DispDirectID = ddf.DispDirect
WHERE d.[DispDirectFactFile] = @FileID and ddf.DispDirectFactID is null

DELETE q FROM [RDS].T_DISP_DIRECT_QUESTIONING q
	join [RDS].T_DISP_DIRECT d on q.DispDirect = d.DispDirectID
	left join [RDS].T_DISP_DIRECT_FACT ddf on d.DispDirectID = ddf.DispDirect
WHERE d.[DispDirectFactFile] = @FileID and ddf.DispDirectFactID is null


DELETE pc FROM [RDS].T_PATIENT_CONTACT pc
	join [RDS].T_DISP_DIRECT d on pc.DispDirect = d.DispDirectID
	left join [RDS].T_DISP_DIRECT_FACT ddf on d.DispDirectID = ddf.DispDirect
WHERE d.[DispDirectFactFile] = @FileID and ddf.DispDirectFactID is null




DELETE d FROM [RDS].T_DISP_DIRECT d--второй этап
	left join [RDS].T_DISP_DIRECT_FACT ddf on d.DispDirectID = ddf.DispDirect
WHERE d.[DispDirectFactFile] = @FileID and ddf.DispDirectFactID is null


DELETE FROM [RDS].T_DISP_DIRECT_FACT_FILE
WHERE FileId = @FileID


END



