delete uc
from T_USER_COLUMNS uc
    join T_USER_CONTEXTS ctx on ctx.UserContextID = uc.UserContextID
where ctx.EntityID in (3001, 501)

delete uc
from T_USER_CONTEXTS uc
where uc.EntityID in (3001, 501)

delete del 
from T_DELETED_OBJECT del
where del.Entities in (3001, 501)

delete us 
from T_USER_SEARCHES us
where us.EntityID in (3001, 501)

delete e
from T_ENTITIES e
where e.EntityID in (3001, 501)
GO

/***** Object:  Table [dbo].[T_SMO_PLAN_REQUEST_STATUS] *****/
CREATE TABLE [dbo].[T_SMO_PLAN_REQUEST_STATUS]
(
[SmoPlanRequestStatusID] uniqueidentifier NOT NULL,
      [PlanRequest] uniqueidentifier NOT NULL,
      [Status] int NOT NULL,
      [Smo] varchar(5) NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[T_SMO_PLAN_REQUEST_STATUS] WITH NOCHECK ADD CONSTRAINT [PK_T_SMO_PLAN_REQUEST_STATUS]
PRIMARY KEY CLUSTERED ([SmoPlanRequestStatusID]) ON [PRIMARY]
GO

ALTER TABLE [dbo].[T_USERS] ADD [Smo] varchar(5)
GO
ALTER TABLE [dbo].[T_USERS] WITH NOCHECK ADD CONSTRAINT [FK_T_SMO_T_USERS_Smo] FOREIGN KEY ([Smo]) REFERENCES [dbo].[T_SMO] ([SMOCOD])
GO

ALTER TABLE [dbo].[T_PLAN_VALUE_SMO] ADD [SmoPlanRequestStatus] uniqueidentifier
GO
ALTER TABLE [dbo].[T_PLAN_VALUE_SMO] WITH NOCHECK ADD CONSTRAINT [FK_T_SMO_PLAN_REQUEST_STATUS_T_PLAN_VALUE_SMO_SmoPlanRequestStatus] FOREIGN KEY ([SmoPlanRequestStatus]) REFERENCES [dbo].[T_SMO_PLAN_REQUEST_STATUS] ([SmoPlanRequestStatusID])
GO





ALTER TABLE [dbo].[T_SMO_PLAN_REQUEST_STATUS] WITH NOCHECK ADD CONSTRAINT [FK_T_PLAN_REQUEST_T_SMO_PLAN_REQUEST_STATUS_PLANREQUEST] FOREIGN KEY([PlanRequest])
REFERENCES [dbo].[T_PLAN_REQUEST] ([PlanID])
GO
ALTER TABLE [dbo].[T_SMO_PLAN_REQUEST_STATUS] WITH NOCHECK ADD CONSTRAINT [FK_T_SMO_T_SMO_PLAN_REQUEST_STATUS_SMO] FOREIGN KEY([Smo])
REFERENCES [dbo].[T_SMO] ([SMOCOD])
GO



--BEGIN TRAN
IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=31)
BEGIN
  INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
  VALUES (31,'USERS',NULL,'Primary SQL Server','Пользователи системы',0, 5)
END
ELSE BEGIN
  UPDATE T_ENTITIES SET EntName='USERS',EntDesc='Пользователи системы',BaseEntity=5 WHERE EntityID=31
END
GO
UPDATE T_ENTITIES SET XML='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entity 
Name="USERS" 
Index="31" 
Description="Пользователи системы" 
version="2" 
DNAttributes="{UserName}" 
DisplayName="{0}" 
UnrestrictedAccess="0" 
Inherits="BASE_USERS">
  <Fields>
    <Parent Name="Mo" Index="310002" Type="MO" Description="Медицинская организация" CharacterMaximumLength="6" />
    <Parent Name="Smo" Index="310003" Type="SMO" Description="Страховая организация" CharacterMaximumLength="5" />
  </Fields>
  <DbMapping>
    <Table database="FIESDB" name="T_USERS">
      <Map Name="Branch" Column="Branch" />
      <Map Name="Mo" Column="Mo" />
      <Map Name="Smo" Column="Smo" />
    </Table>
  </DbMapping>
</Entity>' WHERE EntityID=31
GO
--IF @@ERROR!=0 ROLLBACK ELSE COMMIT

--BEGIN TRAN
IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=540)
BEGIN
  INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
  VALUES (540,'PLAN_REQUEST',NULL,'Primary SQL Server','Заявка на утверждение плановых показателей',0, 539)
END
ELSE BEGIN
  UPDATE T_ENTITIES SET EntName='PLAN_REQUEST',EntDesc='Заявка на утверждение плановых показателей',BaseEntity=539 WHERE EntityID=540
END
GO
UPDATE T_ENTITIES SET XML='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entity 
Name="PLAN_REQUEST" 
Index="540" 
Description="Заявка на утверждение плановых показателей" 
Inherits="PLAN">
  <Fields>
    <Attribute Name="RequestDescription" Index="5400001" Type="string" Description="Примечание" CharacterMaximumLength="512" />
    <Attribute Name="Number" Index="5400004" Type="int" Description="Номер" Required="1" />
    <Attribute Name="RequestDate" Index="5400005" Type="date" Description="Дата отправки" DefaultFormat="{0:dd:MM:yyyy}" />
    <Attribute Name="RequestTime" Index="5400006" Type="datetime" Description="Время отправки" DefaultFormat="{0:HH:mm:ss}" />
    <Child Name="PlanRequestSmos" Index="5400008" Type="PLAN_REQUEST_SMO" Description="Настройки процентных соотношений по СМО" ChildAttribute="PlanRequest" />
    <Child Name="SmoPlanRequestStatuses" Index="5400009" Type="SMO_PLAN_REQUEST_STATUS" Description="Статус заявки в СМО" ChildAttribute="PlanRequest" />
    <Choice Name="Status" Index="5400002" Description="Статус" Required="1">
      <Item Name="Редактирование" Index="0" />
      <Item Name="На утверждении" Index="1" />
      <Item Name="Утвержден частично" Index="3" />
      <Item Name="Не утвержден" Index="4" />
      <Item Name="Утвержден" Index="5" />
    </Choice>
    <Choice Name="TfomsStatus" Index="5400007" Description="Статус утверждения в ТФОМС" Required="1">
      <Item Name="Редактирование" Index="0" />
      <Item Name="На утверждении" Index="1" />
      <Item Name="Утвержден частично" Index="3" />
      <Item Name="Не утвержден" Index="4" />
      <Item Name="Утвержден" Index="5" />
    </Choice>
    <Link Name="PlanNote" Index="5400003" Type="PLAN_NOTE" Description="Замечание на заявку" LinkAttribute="PlanRequest" />
  </Fields>
  <DbMapping>
    <Table database="FIESDB" name="T_PLAN_REQUEST">
      <Map Name="RequestDescription" Column="RequestDescription" />
      <Map Name="Status" Column="Status" />
      <Map Name="Number" Column="Number" />
      <Map Name="RequestDate" Column="RequestDate" />
      <Map Name="RequestTime" Column="RequestTime" />
      <Map Name="TfomsStatus" Column="TfomsStatus" />
    </Table>
  </DbMapping>
</Entity>' WHERE EntityID=540
GO
--IF @@ERROR!=0 ROLLBACK ELSE COMMIT

--BEGIN TRAN
IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=555)
BEGIN
  INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
  VALUES (555,'PLAN_VALUE_SMO',NULL,'Primary SQL Server','Значение параметра',0, NULL)
END
ELSE BEGIN
  UPDATE T_ENTITIES SET EntName='PLAN_VALUE_SMO',EntDesc='Значение параметра',BaseEntity=NULL WHERE EntityID=555
END
GO
UPDATE T_ENTITIES SET XML='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entity 
Name="PLAN_VALUE_SMO" 
Index="555" 
Description="Значение параметра" 
PrimaryKey="PlanValueSmoID">
  <Fields>
    <Attribute Name="PlanValueSmoID" Index="5550001" Type="guid" Description="Идентификатор" />
    <Attribute Name="Value" Index="5550004" Type="int" Description="Значение" />
    <Parent Name="Smo" Index="5550006" Type="SMO" Description="Страховая медицинская организация" CharacterMaximumLength="5" Required="1" />
    <Parent Name="PlanValue" Index="5550007" Type="PLAN_VALUE" Description="Значение параметра" Required="1" />
    <Parent Name="SmoPlanRequestStatus" Index="5550008" Type="SMO_PLAN_REQUEST_STATUS" Description="Статус заявки в СМО" />
    <Link Name="PlanItemMonthValuesSmo" Index="5550005" Type="PLAN_ITEM_MONTH_VALUES_SMO" Description="Разбивка по месяцам по СМО" LinkAttribute="PlanValueSmo" />
  </Fields>
  <DbMapping>
    <Table database="FIESDB" name="T_PLAN_VALUE_SMO">
      <Map Name="PlanValueSmoID" Column="PlanValueSmoID" />
      <Map Name="Value" Column="Value" />
      <Map Name="Smo" Column="Smo" />
      <Map Name="PlanValue" Column="PlanValue" />
      <Map Name="SmoPlanRequestStatus" Column="SmoPlanRequestStatus" />
    </Table>
  </DbMapping>
</Entity>' WHERE EntityID=555
GO
--IF @@ERROR!=0 ROLLBACK ELSE COMMIT

--BEGIN TRAN
IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=501)
BEGIN
  INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
  VALUES (501,'SMO_PLAN_REQUEST_STATUS',NULL,'Primary SQL Server','Статус заявки в СМО',0, NULL)
END
ELSE BEGIN
  UPDATE T_ENTITIES SET EntName='SMO_PLAN_REQUEST_STATUS',EntDesc='Статус заявки в СМО',BaseEntity=NULL WHERE EntityID=501
END
GO
UPDATE T_ENTITIES SET XML='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entity 
Name="SMO_PLAN_REQUEST_STATUS" 
Index="501" 
Description="Статус заявки в СМО" 
PrimaryKey="SmoPlanRequestStatusID">
  <Fields>
    <Attribute Name="SmoPlanRequestStatusID" Index="5010001" Type="guid" Description="Идентификатор" />
    <Parent Name="PlanRequest" Index="5010002" Type="PLAN_REQUEST" Description="Заявка на утверждение плановых показателей" Required="1" />
    <Parent Name="Smo" Index="5010005" Type="SMO" Description="Страховая медицинская организация" CharacterMaximumLength="5" Required="1" />
    <Child Name="PlanValueSmos" Index="5010004" Type="PLAN_VALUE_SMO" Description="Значение параметра" ChildAttribute="SmoPlanRequestStatus" RemoveAction="1" />
    <Choice Name="Status" Index="5010003" Description="Статус заявки" Required="1">
      <Item Name="Редактирование" Index="0" />
      <Item Name="На утверждении" Index="1" />
      <Item Name="Утвержден частично" Index="3" />
      <Item Name="Не утвержден" Index="4" />
      <Item Name="Утвержден" Index="5" />
    </Choice>
  </Fields>
  <DbMapping>
    <Table database="FIESDB" name="T_SMO_PLAN_REQUEST_STATUS">
      <Map Name="SmoPlanRequestStatusID" Column="SmoPlanRequestStatusID" />
      <Map Name="PlanRequest" Column="PlanRequest" />
      <Map Name="Status" Column="Status" />
      <Map Name="Smo" Column="Smo" />
    </Table>
  </DbMapping>
</Entity>' WHERE EntityID=501
GO
--IF @@ERROR!=0 ROLLBACK ELSE COMMIT