create or replace
FUNCTION           ARQUIVO_11_DIEF_MA ( 
	P_CDEMPRESA  IN EMPRESA.CDEMPRESA%TYPE, 
	P_CDINSCESTA IN EMPRESA.CDINSCESTA%TYPE, 
	P_SEQMAGNET  IN VARCHAR, 
	P_CONTADOR   IN NUMBER) 
RETURN VARCHAR 
AS 
   V_CONTADOR NUMBER; 
BEGIN 
   V_CONTADOR   := P_CONTADOR; 
   FOR C_REGISTRO11 IN (SELECT SUBSTR( 
                           '11'|| 
                           RPAD(IE.DSENDEIEST,34,' ')|| 
                           RPAD('00000s/n',27,' ')|| 
                           RPAD(IE.NMBAIRIEST,15,' ')|| 
                           LPAD(REPLACE(REPLACE(IE.NRCEPIEST,'.',''),'-',''),8,'0')|| 
                           RPAD(NVL(IE.NMREPRINSEST, ' '),28,' ')|| 
                           LPAD(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(NVL(IE.NRTELEIEST,'0'),' ',''),')',''),'(',''),'-',''),'.',''),12,'0') 
                           ,1,126) AS DSARQMAG 
                        FROM INSCRESTAD IE 
                        WHERE IE.CDEMPRESA   = P_CDEMPRESA   AND 
                              IE.CDINSCESTA  = P_CDINSCESTA) LOOP 
      V_CONTADOR := NVL(V_CONTADOR,0) + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        (LPAD(V_CONTADOR, 8, '0'), C_REGISTRO11.DSARQMAG, P_SEQMAGNET); 
   END LOOP; 
 RETURN (V_CONTADOR); 
END; 
/
-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_11_DIEF_MA','5.11.026'); END;
/

 
 