IF EXISTS(SELECT NAME 
            FROM SYSOBJECTS
           WHERE NAME = 'SPED_CONTABIL_HISTORICO')
   DROP PROCEDURE SPED_CONTABIL_HISTORICO
GO

CREATE PROCEDURE SPED_CONTABIL_HISTORICO (
   @P_NRSEQARQMAG  VARCHAR(10),
   @P_NRINIORDENA  INTEGER,
   @P_IDSPED       VARCHAR(01), /* PARAMETRO QUE DEFINE SE SERA GERADO SPED OU FCONT */
   @P_NRORDREGARQ  INTEGER OUTPUT)
AS
   DECLARE @V_NRORDREGARQ  INTEGER;
   DECLARE @C_I075_DSLINHA VARCHAR(200);

BEGIN
   SET @V_NRORDREGARQ = @P_NRINIORDENA;

   DECLARE C_I075 CURSOR FOR
      SELECT '|I075|' +
             H.CDHISTPADR + '|' +
             H.DSHISTPADR + '|' AS DSLINHA
        FROM HISTPADR H
       ORDER BY H.CDHISTPADR
   OPEN C_I075;
   FETCH NEXT FROM C_I075 INTO @C_I075_DSLINHA;

   WHILE (@@FETCH_STATUS = 0) BEGIN
      SET @V_NRORDREGARQ = @V_NRORDREGARQ + 1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (RIGHT(REPLICATE('0', 10) + CONVERT(VARCHAR, @V_NRORDREGARQ), 10), @C_I075_DSLINHA, @P_NRSEQARQMAG);

      FETCH NEXT FROM C_I075 INTO @C_I075_DSLINHA;
   END;
   CLOSE C_I075;
   DEALLOCATE C_I075;

   SET @P_NRORDREGARQ = @V_NRORDREGARQ;
END;
GO

-- *VERSAO* --
EXEC ATUVEROBJ 'SPED_CONTABIL_PLANOCONTA','5.11.000';
GO
