delete col
from T_USER_COLUMNS col
    join T_USER_CONTEXTS uc on uc.UserContextID = col.UserContextID
where uc.EntityID = 610

delete uc
from T_USER_CONTEXTS uc
where uc.EntityID = 610

delete e
from T_ENTITIES e
where e.EntityID = 610
GO



--BEGIN TRAN
IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=47)
BEGIN
  INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
  VALUES (47,'DELETED_OBJECT',NULL,'Primary SQL Server','Удаленный объект',0, NULL)
END
ELSE BEGIN
  UPDATE T_ENTITIES SET EntName='DELETED_OBJECT',EntDesc='Удаленный объект',BaseEntity=NULL WHERE EntityID=47
END
GO
UPDATE T_ENTITIES SET XML='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entity 
Name="DELETED_OBJECT" 
Index="47" 
Description="Удаленный объект" 
PrimaryKey="DeletedObjectID">
  <Fields>
    <Attribute Name="DeletedObjectID" Index="470001" Type="guid" Description="Идентификатор" />
    <Attribute Name="DeleteDateTime" Index="470003" Type="datetime" Description="Дата и время" DefaultFormat="{0:dd.MM.yyyy HH:mm}" Required="1" />
    <Attribute Name="ObjectId" Index="470005" Type="string" Description="Идентификатор объекта" CharacterMaximumLength="64" Required="1" />
    <Attribute Name="ObjectInfo" Index="470006" Type="string" Description="Информация об объекте" CharacterMaximumLength="1000" />
    <Parent Name="Users" Index="470002" Type="BASE_USERS" Description="Пользователь системы" Required="1" />
    <Parent Name="Entities" Index="470004" Type="ENTITIES" Description="Сущность" Required="1" />
  </Fields>
  <DbMapping>
    <Table database="FIESDB" name="T_DELETED_OBJECT">
      <Map Name="DeletedObjectID" Column="DeletedObjectID" />
      <Map Name="Users" Column="Users" />
      <Map Name="DeleteDateTime" Column="DeleteDateTime" />
      <Map Name="Entities" Column="Entities" />
      <Map Name="ObjectId" Column="ObjectId" />
      <Map Name="ObjectInfo" Column="ObjectInfo" />
    </Table>
  </DbMapping>
</Entity>' WHERE EntityID=47
GO
--IF @@ERROR!=0 ROLLBACK ELSE COMMIT



ALTER TABLE [dbo].[T_DELETED_OBJECT] ALTER COLUMN [ObjectId] varchar(64) NOT NULL
GO
ALTER TABLE [dbo].[T_DELETED_OBJECT] ADD [ObjectInfo] varchar(1000)
GO



-- Tables
/***** Object:  Table [dbo].[T_DELETED_OBJECT_INFO] *****/
CREATE TABLE [dbo].[T_DELETED_OBJECT_INFO]
(
  [DeletedObjectInfoID] uniqueidentifier NOT NULL,
  [Entities] int NOT NULL,
  [Lexem] text NOT NULL
) ON [PRIMARY]
GO

-- Alters

ALTER TABLE [dbo].[T_DELETED_OBJECT_INFO] WITH NOCHECK ADD CONSTRAINT [PK_T_DELETED_OBJECT_INFO]
PRIMARY KEY CLUSTERED ([DeletedObjectInfoID]) ON [PRIMARY]
GO

ALTER TABLE [dbo].[T_DELETED_OBJECT_INFO] WITH NOCHECK ADD CONSTRAINT [FK_T_ENTITIES_T_DELETED_OBJECT_INFO_ENTITIES] FOREIGN KEY([Entities])
REFERENCES [dbo].[T_ENTITIES] ([EntityID])
GO

