IF EXISTS(SELECT * FROM sysobjects O, sysusers U
          WHERE
            o.name = 'GRAVA_IDITMCONS'  and
            type = 'TR'                 and
            u.uid = o.uid               and
            u.name = USER)
  DROP TRIGGER GRAVA_IDITMCONS
  GO

CREATE TRIGGER GRAVA_IDITMCONS
ON ITEMSOLI
FOR UPDATE
AS

BEGIN
  UPDATE ITEMSOLI
  SET IDITMCONS = 'N'
  FROM
    INSERTED,
    DELETED
  WHERE
    INSERTED.CDFILSOLI   = DELETED.CDFILSOLI   AND
    INSERTED.CDFILLAN    = DELETED.CDFILLAN    AND
    INSERTED.NRSOLICMP   = DELETED.NRSOLICMP   AND
    INSERTED.CDPRODUTO   = DELETED.CDPRODUTO   AND
    INSERTED.DTPROGENTR  = DELETED.DTPROGENTR  AND
    INSERTED.CDFILSOLI   = ITEMSOLI.CDFILSOLI  AND
    INSERTED.CDFILLAN    = ITEMSOLI.CDFILLAN   AND
    INSERTED.NRSOLICMP   = ITEMSOLI.NRSOLICMP  AND
    INSERTED.CDPRODUTO   = ITEMSOLI.CDPRODUTO  AND
    INSERTED.DTPROGENTR  = ITEMSOLI.DTPROGENTR AND
    INSERTED.NRSOLCMPCONS IS NULL              AND
    DELETED.NRSOLCMPCONS  IS NOT NULL;
END;
GO
