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

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

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

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



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

IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[EHR].[EHR_RUS_TREE_AfterInsert]'))
DROP TRIGGER [EHR].[EHR_RUS_TREE_AfterInsert]
GO

IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[EHR].[EHR_RUS_TREE_AfterDelete]'))
DROP TRIGGER [EHR].[EHR_RUS_TREE_AfterDelete]
GO

IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[EHR].[EHR_RUS_TREE_ChangeParent]'))
DROP TRIGGER [EHR].[EHR_RUS_TREE_ChangeParent]
GO

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

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

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


DELETE er FROM EHR.T_EHR_REFERENCE er join VCLib.T_ENTITIES e on er.EhrEntity = e.EntityID
WHERE e.EntName = 'EHR_RUS_TREE'

DELETE er FROM EHR.T_EHR_REFERENCE er join VCLib.T_ENTITIES e on er.EhrEntity = e.EntityID
WHERE e.EntName = 'EHR_RUS'


DROP TABLE EHR.T_EHR_RUS_TREE

DROP TABLE EHR.T_EHR_RUS


DECLARE @EntityEHR_RUS int
DECLARE @EntityEHR_RUS_TREE int
SELECT @EntityEHR_RUS = EntityID FROM VCLib.T_ENTITIES e WHERE e.EntName = 'EHR_RUS' and e.[Schema] = 'EHR'
SELECT @EntityEHR_RUS_TREE = EntityID FROM VCLib.T_ENTITIES e WHERE e.EntName = 'EHR_RUS_TREE' and e.[Schema] = 'EHR'
UPDATE e SET BaseEntity = null FROM VCLib.T_ENTITIES e WHERE BaseEntity = @EntityEHR_RUS or BaseEntity = @EntityEHR_RUS_TREE



select EntityID 
INTO #ENTITY
from VCLib.T_ENTITIES e
where e.EntName in (
'EHR_RUS',
'EHR_RUS_TREE'
)
and e.[Schema] = 'EHR'

Delete col
From VCLib.T_USER_COLUMNS col
	Join VCLib.T_USER_CONTEXTS ctx On col.UserContextID = ctx.UserContextID 
	join #ENTITY e on  ctx.EntityID = e.EntityID

Delete ctx
From VCLib.T_USER_CONTEXTS ctx
	join #ENTITY e on  ctx.EntityID = e.EntityID

delete del 
from VCLib.T_DELETED_OBJECT del
	join #ENTITY e on  del.Entities = e.EntityID

delete fv
From VCLib.T_TEMPLATE_FIELD_VALUE fv
Join VCLib.T_TEMPLATE_FIELD fld On fv.TemplateField = fld.TemplateFieldID
	join #ENTITY e on  fld.Entity = e.EntityID

delete fld
From VCLib.T_TEMPLATE_FIELD fld
	join #ENTITY e on  fld.Entity = e.EntityID
		
delete tf
From VCLib.T_DATAFORM_TEMPLATE tf
	join #ENTITY e on  tf.Entities = e.EntityID
		
delete vc
From VCLib.T_DATAFORM_VALIDATION dv
	join #ENTITY e on  dv.Entities = e.EntityID
	join VCLib.T_VALIDATION_CONDITION vc on  vc.DataformValidation = dv.Entities
		
delete dv
From VCLib.T_DATAFORM_VALIDATION dv
	join #ENTITY e on  dv.Entities = e.EntityID

delete rep
From VCLib.T_Report rep
	join #ENTITY e on  rep.EntityID = e.EntityID

delete g_rep
    FROM VCLib.T_GENERIC_REPORT g_rep
	join #ENTITY e on  g_rep.Entities = e.EntityID

DELETE us 
FROM VCLib.T_USER_SEARCHES  us
	join #ENTITY e on  us.EntityID = e.EntityID

delete eucr
from VCLib.T_EXTRA_UI_COMMAND_ROLES eucr
    join VCLib.T_EXTRA_UI_COMMAND EUC on EUC.ExtraUiCommandID = eucr.ExtraUiCommand
    JOIN #ENTITY ET ON ET.EntityID = EUC.CommandEntity

DELETE EUC
FROM VCLib.T_EXTRA_UI_COMMAND EUC
    JOIN #ENTITY ET ON ET.EntityID = EUC.CommandEntity

delete e 
from VCLib.T_ENTITIES_ATTRIBUTES e
	join #ENTITY ee on  e.EntityID = ee.EntityID

delete e 
from VCLib.T_ENTITIES e
	join #ENTITY ee on  e.EntityID = ee.EntityID

drop table #ENTITY


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_UpdateForeignKeys]') and xtype = N'P')
drop procedure [dbo].sp_UpdateForeignKeys
GO