DROP TABLE AKUZ.T_CDA_ORGANIZATION_CONTACTS_HEMOTRANSFUSION_LINK
GO

DROP TABLE AKUZ.T_CDA_SVEDEN_OB_HEMOTRANSFUSION 
GO

select EntityID 
INTO #ENTITY
from VCLib.T_ENTITIES e
where e.EntName in (
'CDA_ORGANIZATION_CONTACTS_HEMOTRANSFUSION_LINK',
'CDA_SVEDEN_OB_HEMOTRANSFUSION'
)
and e.[Schema] = 'AKUZ'

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 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 vc
From VCLib.T_DATAFORM_VALIDATION dv
	join VCLib.T_VALIDATION_CONDITION vc on  vc.DataformValidation = dv.Entities
	join #ENTITY e on  dv.Entities = e.EntityID

delete dv
from VCLib.T_DATAFORM_VALIDATION dv
	join #ENTITY e on  dv.Entities = e.EntityID
	
delete dt
from VCLib.T_DATAFORM_TEMPLATE dt
	join #ENTITY e on  dt.Entities = e.EntityID

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
GO