 -- Tables  /***** Object:  Table [dbo].[T_PERIOD_SECTION] *****/
     CREATE TABLE [dbo].[T_PERIOD_SECTION]
     (
 [PeriodSectionID] uniqueidentifier NOT NULL,
             [DateEnd] datetime NULL,
             [DateBegin] datetime NOT NULL,
             [Name] varchar(250) NOT NULL,
             [Priority] int NOT NULL,
             [PeriodAlias] varchar(250) NOT NULL
     ) ON [PRIMARY]
     GO
 -- Alters
         ALTER TABLE [dbo].[T_PERIOD_SECTION] WITH NOCHECK ADD CONSTRAINT [PK_T_PERIOD_SECTION]
         PRIMARY KEY CLUSTERED ([PeriodSectionID]) ON [PRIMARY]
         GO
          
/****** Object:  View [dbo].[V_VISIT_PLAN_ITEM_VIEW_SMO]    Script Date: 07.06.2016 15:53:27 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_VISIT_PLAN_ITEM_VIEW_SMO]'))
DROP VIEW [dbo].[V_VISIT_PLAN_ITEM_VIEW_SMO]
GO
/****** Object:  View [dbo].[V_VISIT_PLAN_VALUE_VIEW_SMO]    Script Date: 07.06.2016 15:53:36 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_VISIT_PLAN_VALUE_VIEW_SMO]'))
DROP VIEW [dbo].[V_VISIT_PLAN_VALUE_VIEW_SMO]
GO

drop table T_SMO_COEF_MONTH_VALUES
drop table T_VISIT_PLAN_ITEM_SMO_VALUES
drop table T_VISIT_PLAN_MONTH_VALUES
drop table T_VISIT_PLAN_ITEM_SMO_SUB_ITEM
drop table T_VISIT_PLAN_ITEM_SMO
drop table T_VISIT_PLAN_VALUE
drop table T_VISIT_PLAN_ITEM
drop table T_VISIT_PLAN_REQUEST
drop table T_VISIT_PLAN

INSERT INTO [dbo].[T_PERIOD_SECTION]
           ([PeriodSectionID]
           ,[DateEnd]
           ,[DateBegin]
           ,[Name]
           ,[Priority]
           ,[PeriodAlias])
Select newID(), null, '20120101', 'Январь', 1, 'JAN'
union Select newID(), null, '20120101', 'Февраль', 2, 'FEB'
union Select newID(), null, '20120101', 'Март', 3, 'MAR'
union Select newID(), null, '20120101', 'Апрель', 4, 'APR'
union Select newID(), null, '20120101', 'Май', 5, 'MAY'
union Select newID(), null, '20120101', 'Июнь', 6, 'JUN'
union Select newID(), null, '20120101', 'Июль', 7, 'JUL'
union Select newID(), null, '20120101', 'Август', 8, 'AUG'
union Select newID(), null, '20120101', 'Сентябрь', 9, 'SEP'
union Select newID(), null, '20120101', 'Октябрь', 10, 'OCT'
union Select newID(), null, '20120101', 'Ноябрь', 11, 'NOV'
union Select newID(), null, '20120101', 'Декабрь', 12, 'DEC'
GO

delete dus
from T_DICTIONARY_USER dus
join T_DICTIONARY_UPDATE du on du.DictionaryUpdateID = dus.DictionaryUpdate
join T_ENTITIES e on e.EntityID = du.Entities
where EntName = 'SMO_COEF_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_VALUES'
							or 	EntName = 'VISIT_PLAN_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_SUB_ITEM'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO'
							or 	EntName = 'VISIT_PLAN_VALUE'
							or 	EntName = 'VISIT_PLAN_ITEM'
							or 	EntName = 'VISIT_PLAN_REQUEST'
							or 	EntName = 'VISIT_PLAN'

delete du
from T_DICTIONARY_UPDATE du
join T_ENTITIES e on e.EntityID = du.Entities
where EntName = 'SMO_COEF_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_VALUES'
							or 	EntName = 'VISIT_PLAN_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_SUB_ITEM'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO'
							or 	EntName = 'VISIT_PLAN_VALUE'
							or 	EntName = 'VISIT_PLAN_ITEM'
							or 	EntName = 'VISIT_PLAN_REQUEST'
							or 	EntName = 'VISIT_PLAN'
delete rf
from T_REPORTFILE rf
join T_REPORTFILEGROUP rfg on rfg.GroupID = rf.GroupID
join T_REPORT r on r.ReportID = rfg.ReportID
join T_ENTITIES e on e.EntityID = r.EntityID
where EntName = 'SMO_COEF_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_VALUES'
							or 	EntName = 'VISIT_PLAN_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_SUB_ITEM'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO'
							or 	EntName = 'VISIT_PLAN_VALUE'
							or 	EntName = 'VISIT_PLAN_ITEM'
							or 	EntName = 'VISIT_PLAN_REQUEST'
							or 	EntName = 'VISIT_PLAN'
delete rfg 
from T_REPORTFILEGROUP rfg
join T_REPORT r on r.ReportID = rfg.ReportID
join T_ENTITIES e on e.EntityID = r.EntityID
where EntName = 'SMO_COEF_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_VALUES'
							or 	EntName = 'VISIT_PLAN_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_SUB_ITEM'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO'
							or 	EntName = 'VISIT_PLAN_VALUE'
							or 	EntName = 'VISIT_PLAN_ITEM'
							or 	EntName = 'VISIT_PLAN_REQUEST'
							or 	EntName = 'VISIT_PLAN'

delete r 
from T_REPORT r
join T_ENTITIES e on e.EntityID = r.EntityID
where EntName = 'SMO_COEF_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_VALUES'
							or 	EntName = 'VISIT_PLAN_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_SUB_ITEM'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO'
							or 	EntName = 'VISIT_PLAN_VALUE'
							or 	EntName = 'VISIT_PLAN_ITEM'
							or 	EntName = 'VISIT_PLAN_REQUEST'
							or 	EntName = 'VISIT_PLAN'

delete from T_ENTITIES where EntName = 'SMO_COEF_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_VALUES'
							or 	EntName = 'VISIT_PLAN_MONTH_VALUES'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO_SUB_ITEM'
							or 	EntName = 'VISIT_PLAN_ITEM_SMO'
							or 	EntName = 'VISIT_PLAN_VALUE'
							or 	EntName = 'VISIT_PLAN_ITEM'
							or 	EntName = 'VISIT_PLAN_REQUEST'
							or 	EntName = 'VISIT_PLAN'
							or 	EntName = 'VISIT_PLAN_ITEM_VIEW_SMO'
							or 	EntName = 'VISIT_PLAN_VALUE_VIEW_SMO'
