Use BUH

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tg_spravka_U]') and OBJECTPROPERTY(id, N'IsTrigger') = 1)
drop trigger [dbo].[tg_spravka_U]
GO
CREATE TRIGGER tg_spravka_U ON [dbo].[tbSpravka]
FOR UPDATE
AS
BEGIN

--if trigger_nestlevel ( object_ID( 't1' ) ) > 1 return


 declare @NUMDOCPART varchar(10)
 declare @DATEDOCPART datetime
 declare @MES datetime
 declare @NDSDT int
 declare @NDSKT int
 declare @SUMM decimal(18,3)
 declare @IDPREDPR int
 declare @OTMOPLATY bit

Declare @NumZap integer, @Sv bit


IF ( (trigger_nestlevel ( object_ID( 'tg_spravka_U' )) <= 1) AND (UPDATE(Summ)) )  BEGIN

declare c_ins CURSOR LOCAL FOR
 SELECT I.NumDocPart, I.DataDocPart, I.Mes, I.idNDSDT, I.idNDSKT, I.Summ, I.idPredpr,I.OtmOplaty
 FROM  Inserted I
        

 OPEN c_ins
 FETCH NEXT FROM c_ins INTO @NUMDOCPART, @DATEDOCPART, @MES, @NDSDT, @NDSKT, @SUMM,  @IDPREDPR,@OTMOPLATY

 WHILE (@@fetch_status <> -1) BEGIN
    IF ( (@NDSDT IS NULL) AND (@NDSKT IS NULL) ) BEGIN

       SELECT @SUMM = SUM(Summ)
        FROM tbSpravka
        WHERE IsNull(NumDocPart,'') = Isnull(@NUMDOCPART,'')
              AND IsNull(DataDocPart,'') =  IsNull(@DATEDOCPART,'')
              AND (MONTH(Mes) =  MONTH(@MES) AND YEAR(Mes)=YEAR(@MES))
              AND IsNull(idPredpr,'') = IsNull(@IDPREDPR,'')
              AND OtmOplaty=1
              AND ( (idNDSDT IS NOT NULL) OR (idNDSKT IS NOT NULL) )

        UPDATE tbSpravka
        SET SumSNDS = Summ + @SUMM
        WHERE IsNull(NumDocPart,'') = Isnull(@NUMDOCPART,'')
              AND IsNull(DataDocPart,'') =  IsNull(@DATEDOCPART,'')
              AND (MONTH(Mes) =  MONTH(@MES) AND YEAR(Mes)=YEAR(@MES))
              AND IsNull(idPredpr,'') = IsNull(@IDPREDPR,'')
              AND OtmOplaty=1
              AND (idNDSDT IS NULL
              AND idNDSKT IS NULL)
      END ELSE BEGIN
SELECT @SUMM = SUM(Summ)
      FROM tbSpravka
       WHERE IsNull(NumDocPart,'') = Isnull(@NUMDOCPART,'')
              AND IsNull(DataDocPart,'') =  IsNull(@DATEDOCPART,'')
              AND (MONTH(Mes) =  MONTH(@MES) AND YEAR(Mes)=YEAR(@MES))
              AND IsNull(idPredpr,'') = IsNull(@IDPREDPR,'')
              AND OtmOplaty=1
           
        UPDATE tbSpravka
        SET SumSNDS = @SUMM 
        WHERE IsNull(NumDocPart,'') = Isnull(@NUMDOCPART,'')
            AND IsNull(DataDocPart,'') =  IsNull(@DATEDOCPART,'')
            AND (MONTH(Mes) =  MONTH(@MES) AND YEAR(Mes)=YEAR(@MES))
            AND IsNull(idPredpr,'') = IsNull(@IDPREDPR,'')
            AND OtmOplaty=1
            AND (idNDSDT IS NULL
            AND idNDSKT IS NULL)

    END
    FETCH NEXT FROM c_ins INTO @NUMDOCPART, @DATEDOCPART, @MES, @NDSDT, @NDSKT,  @SUMM, @IDPREDPR,@OTMOPLATY
 END

 CLOSE c_ins
 DEALLOCATE c_ins
END

----27.10.2016
 IF ( (trigger_nestlevel ( object_ID( 'tg_spravka_U' )) <= 1) AND (UPDATE(Sv) ))
 BEGIN
declare c_ins CURSOR LOCAL FOR
 SELECT I.NumDocPart, I.DataDocPart , I.idPredpr,I.Sv
 FROM  Inserted I
        

 OPEN c_ins
 FETCH NEXT FROM c_ins INTO @NUMDOCPART, @DATEDOCPART,   @IDPREDPR,@Sv

 WHILE (@@fetch_status <> -1) BEGIN


if @Sv=1

Update tbSpravka  set Sv=1
    where IdPredpr=@IdPredpr and IsNull(NumDocPart,NumDoc)=@NumDocPart
                  and IsNull(DataDocPart,DataDoc)=@DateDocPart  and Sv=0

 FETCH NEXT FROM c_ins INTO @NUMDOCPART, @DATEDOCPART,   @IDPREDPR,@Sv
END
 CLOSE c_ins
 DEALLOCATE c_ins
END
END
