-- 1.1.11 Power.TechnologicalConnection

-- Меняем модель данных "дополнительная информация" теперь относится не к Разграничению, а к Объекту электроснабжения.
declare @table_name nvarchar(50)
declare @strSQL nvarchar(1024)

set @table_name=N'DelineationsExtraInfo'
set @strSQL=''
select @strSQL = @strSQL + ' ALTER TABLE ' + @table_name + ' DROP CONSTRAINT '+ object_name(FK_Tab.id) 
from sysobjects FK_Tab 
where (xtype = 'F') AND object_id(@table_name)=parent_obj 
if @strSQL<>''  exec (@strSQL)
GO
delete From DelineationsExtraInfo
	where not exists (select DelineationID From ConsumingObjects where DelineationsExtraInfo.DelineationID = ConsumingObjects.DelineationID)	
GO
Update DelineationsExtraInfo set DelineationsExtraInfo.DelineationID = ConsumingObjects.ID
	From DelineationsExtraInfo left join ConsumingObjects on DelineationsExtraInfo.DelineationID = ConsumingObjects.DelineationID
GO
EXEC sp_rename 'DelineationsExtraInfo.DelineationID', 'ConsumingObjectID'
GO
EXEC sp_rename 'DelineationsExtraInfo', 'ConsumingObjectExtraInfo'
GO
ALTER TABLE ConsumingObjectExtraInfo ADD CONSTRAINT ConsumingObjectExtraInfoConsumingObject_fk 
	FOREIGN KEY (ConsumingObjectID)
	REFERENCES ConsumingObjects (ID)
	ON DELETE CASCADE
        ON UPDATE CASCADE
GO
alter table ConsumingObjectExtraInfo add AttachId int
GO
ALTER TABLE ConsumingObjectExtraInfo
	ADD CONSTRAINT ConsumingObjectExtraInfoDocument_fk FOREIGN KEY (AttachId) 
		REFERENCES StoreDocuments (DocumentID)
		ON DELETE CASCADE
GO
insert into StoreDocumentTypes(StoreDocumentTypeID, Description) Values(10, 'ConsumingObjectExtraInfo')
GO
-- Исправление ошибки заполнения таблицы.
update StoreDocumentTypes set Description = 'SealAccount'
where StoreDocumentTypeID = 8

update StoreDocumentTypes set Description = 'InspectionEPU'
where StoreDocumentTypeID = 9