IF EXISTS(SELECT NAME
          FROM SYSOBJECTS
          WHERE NAME = 'REVERTE_LOTE' AND TYPE = 'P')
   DROP PROCEDURE REVERTE_LOTE
   GO

CREATE PROCEDURE REVERTE_LOTE
   @P_CDEMPRESA VARCHAR(02),
   @P_CDLOTE    VARCHAR(20)

AS

BEGIN

   UPDATE CAPALOTE
   SET CAPALOTE.IDLOTE = 'D'
   WHERE (CAPALOTE.CDEMPRESA = @P_CDEMPRESA) AND
         (CAPALOTE.CDLOTE    = @P_CDLOTE);

   DELETE LANCCONT
   WHERE (LANCCONT.CDEMPRESA = @P_CDEMPRESA)  AND
         (LANCCONT.NRLANCCONT) IN (SELECT DISTINCT LANCLOTE.NRLANCDEFI
                                   FROM LANCLOTE
                                   WHERE (LANCLOTE.CDEMPRESA = @P_CDEMPRESA) AND
                                         (LANCLOTE.CDLOTE    = @P_CDLOTE)  );

/*   COMMIT;
   EXCEPTION
      WHEN OTHERS THEN
         ROLLBACK;
         RAISE;*/

END;
GO


-- VERSAO --
EXEC ATUVEROBJ 'REVERTE_LOTE','5.05.020';
GO

