IF EXISTS(SELECT *
          FROM SYSOBJECTS O, SYSUSERS U
          WHERE O.NAME = 'LOG_FILIAPADRAO' AND
                TYPE   = 'TR'              AND
                U.UID  = O.UID             AND
                U.NAME = USER)

DROP TRIGGER LOG_FILIAPADRAO
GO

CREATE TRIGGER LOG_FILIAPADRAO
ON PARAMFILIPADR
FOR UPDATE, INSERT, DELETE
AS
BEGIN
  DECLARE @V_USUARIO          VARCHAR(40);
  DECLARE @V_DSREMOTEADDR     VARCHAR(15);
  DECLARE @V_DSREMOTEHOST     VARCHAR(15);
  DECLARE @V_DSSERVERRDM      VARCHAR(20);
  DECLARE @V_DSSERVMETHOD     VARCHAR(30);
  DECLARE @V_DSREMOTEOPER     VARCHAR(12);
  DECLARE @V_NMOPERADOR       VARCHAR(40);
  
  DECLARE @V_TIPO               VARCHAR(1);
  DECLARE @V_CDFILIAL_NEW       VARCHAR(4);
  DECLARE @V_NMTABPARAMFILI_NEW VARCHAR(30);
  DECLARE @V_IDTABPARAMFILI_NEW VARCHAR(1);
  DECLARE @V_CDFILIAL_OLD       VARCHAR(4);
  DECLARE @V_NMTABPARAMFILI_OLD VARCHAR(30);
  DECLARE @V_IDTABPARAMFILI_OLD VARCHAR(1);

  DECLARE @V_DESC    VARCHAR(300);
  DECLARE @V_TPMOVI  VARCHAR(1);
  DECLARE @V_ITPESQ           VARCHAR(40);
  DECLARE @V_CDFILLOGOPER     VARCHAR(4);
  DECLARE @V_DTOPER           DATETIME;

   SET @V_DSREMOTEADDR = ' ';
   SET @V_DSREMOTEHOST = ' ';
   SET @V_DSSERVERRDM  = ' ';
   SET @V_DSSERVMETHOD = ' ';
   SET @V_DSREMOTEOPER = ' ';
   SET @V_NMOPERADOR   = ' ';


   EXEC NMUSUARIO @V_NMOPERADOR;
   EXEC WEBTUNNING @V_DSREMOTEADDR,
                   @V_DSREMOTEHOST,
                   @V_DSSERVERRDM,
                   @V_DSSERVMETHOD,
                   @V_DSREMOTEOPER;

   SET  @V_DESC = ''; 
   DECLARE C_CURSOR_IT CURSOR FOR
   SELECT
      'U' IDCOMANDO,
      INSERTED.CDFILIAL,
      INSERTED.NMTABPARAMFILI,
      INSERTED.IDTABPARAMFILI,
      DELETED.CDFILIAL,
      DELETED.NMTABPARAMFILI,
      DELETED.IDTABPARAMFILI
   FROM
      INSERTED,
      DELETED
   WHERE
      INSERTED.CDFILIAL       = DELETED.CDFILIAL    AND
      INSERTED.NMTABPARAMFILI = DELETED.NMTABPARAMFILI;

   OPEN C_CURSOR_IT;
   FETCH NEXT FROM C_CURSOR_IT INTO @V_TIPO, 
                                    @V_CDFILIAL_NEW,
                                    @V_NMTABPARAMFILI_NEW,
                                    @V_IDTABPARAMFILI_NEW,
                                    @V_CDFILIAL_OLD,
                                    @V_NMTABPARAMFILI_OLD,
                                    @V_IDTABPARAMFILI_OLD;
   WHILE (@@FETCH_STATUS = 0) BEGIN
      IF @V_TIPO = 'U' BEGIN
         IF (ISNULL(@V_IDTABPARAMFILI_OLD,'!')   <> ISNULL(@V_IDTABPARAMFILI_NEW,'!')) BEGIN
            SET @V_DESC = 'IdTabParamFili=' + @V_IDTABPARAMFILI_NEW;
            SET @V_TPMOVI         = '2';
            SET @V_ITPESQ       = @V_CDFILIAL_OLD + @V_NMTABPARAMFILI_OLD ;
            SET @V_CDFILLOGOPER = @V_CDFILIAL_OLD;
         END;
      END;

      EXEC NMUSUARIO @V_USUARIO;
      SET @V_USUARIO = ISNULL(@V_USUARIO, ' ');

      IF @V_TPMOVI <> '0' BEGIN
         IF  @V_DESC IS NOT NULL BEGIN  
            IF NOT @V_USUARIO IS NULL BEGIN
               SET @V_DTOPER = GETDATE();
               INSERT INTO LOGOPERA
                  (NMTABELA,   DTOPERACAO, NMRESPOPER, IDTIPOOPER, CDPESQITEM, DSOPERACAO, CDFILLOGOPER,
                   DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
               VALUES
                  ('PARAMFILIPADR',   @V_DTOPER, @V_USUARIO,  @V_TPMOVI, @V_ITPESQ, @V_DESC, @V_CDFILLOGOPER,
                   @V_DSREMOTEADDR, @V_DSREMOTEHOST, @V_DSSERVERRDM, @V_DSSERVMETHOD, @V_DSREMOTEOPER);
            END;
         END;
      END;
      FETCH NEXT FROM C_CURSOR_IT INTO @V_TIPO, 
                                       @V_CDFILIAL_NEW,
                                       @V_NMTABPARAMFILI_NEW,
                                       @V_IDTABPARAMFILI_NEW,
                                       @V_CDFILIAL_OLD,
                                       @V_NMTABPARAMFILI_OLD,
                                       @V_IDTABPARAMFILI_OLD;

   END;
   CLOSE C_CURSOR_IT;
   DEALLOCATE C_CURSOR_IT;      
END;
GO

-- VERSAO --
EXEC ATUVEROBJ 'LOG_FILIAPADRAO','5.07.006';
GO
