SELECT * INTO FIES.T_MONTH_VALUES_BACKUP FROM FIES.T_MONTH_VALUES WHERE type_ IN (SELECT EntityID FROM VCLib.T_ENTITIES
												WHERE EntName = 'PLAN_REQUEST_SMO_ITEM' 
													OR EntName = 'SMO_COEF_MONTH_VALUES_EXCEPTION'
													OR EntName = 'SMO_COEF_MONTH_VALUES_DEFAULT');
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FIES].[FK_T_MONTH_VALUES_T_PLAN_REQUEST_SMO_ITEM_MONTHVALUESID]') AND parent_object_id = OBJECT_ID(N'[FIES].[T_PLAN_REQUEST_SMO_ITEM]'))
ALTER TABLE [FIES].[T_PLAN_REQUEST_SMO_ITEM] DROP CONSTRAINT [FK_T_MONTH_VALUES_T_PLAN_REQUEST_SMO_ITEM_MONTHVALUESID]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FIES].[FK_T_PLAN_REQUEST_SMO_T_PLAN_REQUEST_SMO_ITEM_PLANREQUESTSMO]') AND parent_object_id = OBJECT_ID(N'[FIES].[T_PLAN_REQUEST_SMO_ITEM]'))
ALTER TABLE [FIES].[T_PLAN_REQUEST_SMO_ITEM] DROP CONSTRAINT [FK_T_PLAN_REQUEST_SMO_T_PLAN_REQUEST_SMO_ITEM_PLANREQUESTSMO]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FIES].[FK_T_MO_T_SMO_COEF_MONTH_VALUES_DEFAULT_MO]') AND parent_object_id = OBJECT_ID(N'[FIES].[T_SMO_COEF_MONTH_VALUES_DEFAULT]'))
ALTER TABLE [FIES].[T_SMO_COEF_MONTH_VALUES_DEFAULT] DROP CONSTRAINT [FK_T_MO_T_SMO_COEF_MONTH_VALUES_DEFAULT_MO]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FIES].[FK_T_MONTH_VALUES_T_SMO_COEF_MONTH_VALUES_DEFAULT_MONTHVALUESID]') AND parent_object_id = OBJECT_ID(N'[FIES].[T_SMO_COEF_MONTH_VALUES_DEFAULT]'))
ALTER TABLE [FIES].[T_SMO_COEF_MONTH_VALUES_DEFAULT] DROP CONSTRAINT [FK_T_MONTH_VALUES_T_SMO_COEF_MONTH_VALUES_DEFAULT_MONTHVALUESID]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FIES].[FK_T_SMO_T_SMO_COEF_MONTH_VALUES_DEFAULT_SMO]') AND parent_object_id = OBJECT_ID(N'[FIES].[T_SMO_COEF_MONTH_VALUES_DEFAULT]'))
ALTER TABLE [FIES].[T_SMO_COEF_MONTH_VALUES_DEFAULT] DROP CONSTRAINT [FK_T_SMO_T_SMO_COEF_MONTH_VALUES_DEFAULT_SMO]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FIES].[FK_T_MO_T_SMO_COEF_MONTH_VALUES_EXCEPTION_MO]') AND parent_object_id = OBJECT_ID(N'[FIES].[T_SMO_COEF_MONTH_VALUES_EXCEPTION]'))
ALTER TABLE [FIES].[T_SMO_COEF_MONTH_VALUES_EXCEPTION] DROP CONSTRAINT [FK_T_MO_T_SMO_COEF_MONTH_VALUES_EXCEPTION_MO]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FIES].[FK_T_MONTH_VALUES_T_SMO_COEF_MONTH_VALUES_EXCEPTION_MONTHVALUESID]') AND parent_object_id = OBJECT_ID(N'[FIES].[T_SMO_COEF_MONTH_VALUES_EXCEPTION]'))
ALTER TABLE [FIES].[T_SMO_COEF_MONTH_VALUES_EXCEPTION] DROP CONSTRAINT [FK_T_MONTH_VALUES_T_SMO_COEF_MONTH_VALUES_EXCEPTION_MONTHVALUESID]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FIES].[FK_T_SMO_T_SMO_COEF_MONTH_VALUES_EXCEPTION_SMO]') AND parent_object_id = OBJECT_ID(N'[FIES].[T_SMO_COEF_MONTH_VALUES_EXCEPTION]'))
ALTER TABLE [FIES].[T_SMO_COEF_MONTH_VALUES_EXCEPTION] DROP CONSTRAINT [FK_T_SMO_T_SMO_COEF_MONTH_VALUES_EXCEPTION_SMO]
GO

DELETE FROM FIES.T_MONTH_VALUES WHERE type_ IN (SELECT EntityID FROM VCLib.T_ENTITIES
												WHERE EntName = 'PLAN_REQUEST_SMO_ITEM' 
													OR EntName = 'SMO_COEF_MONTH_VALUES_EXCEPTION'
													OR EntName = 'SMO_COEF_MONTH_VALUES_DEFAULT');
