---- Подстанция - AFTER UPDATE,DELETE ----
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OnПодстанцияDelete]') and xtype = N'TR')
drop trigger [dbo].[OnПодстанцияDelete]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AfterПодстанцияUpdate]') and xtype = N'TR')
drop trigger [dbo].[AfterПодстанцияUpdate]
GO
CREATE TRIGGER AfterПодстанцияUpdate ON [dbo].[Подстанция] 
AFTER UPDATE,DELETE
AS
  if (SELECT Count(AddressID) FROM DELETED) > 0
    DELETE FROM AddressBaseParts WHERE 
      AddressID in (select AddressID from DELETED)
      AND ((select count(AddressID) from inserted) = 0 OR   
      AddressID not in (select AddressID from INSERTED))  
