IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[ISZL].[INSURED_POLICY_RECORD_autogenerated]'))
DROP TRIGGER [ISZL].[INSURED_POLICY_RECORD_autogenerated]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER ISZL.INSURED_POLICY_RECORD_autogenerated
   ON ISZL.T_INSURED_POLICY_RECORD
   AFTER UPDATE, INSERT
AS 
BEGIN
    SET NOCOUNT ON
    if (UPDATE(Insured) or (select count(*) from INSERTED) > 0)
    begin
        update ins
        set ins.AutoGenerated = 0
		from ISZL.T_INSURED ins
		where ins.InsuredID in(select Insured from INSERTED)
    end
END
GO

ALTER TABLE [ISZL].[T_INSURED_POLICY_RECORD] ENABLE TRIGGER [INSURED_POLICY_RECORD_fio]
GO
