miércoles, 30 de noviembre de 2011

GENERATE SUBROUTINE POOL


En ocasiones cuando se requiere del máximo dinamismo en un programa ABAP es necesario recurrir a ese tipo de programas, tiene algunas limitantes en la ejecución pero si encapsulas su ejecución es un problema resuelto. esto para evitar el error por superar las 36 ejecuciones

Ejemplo, ejecutar el siguiente SQL:

select * @into corresponding fields of table l_icon @Where NAME = ‘ICON_PRESENCE’.


Function z_execute_sql.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     VALUE(SQL) TYPE  STRING
*"  TABLES
*"      IT_RETURN STRUCTURE  ICON OPTIONAL
*"----------------------------------------------------------------------
  field-symbols: <l_ref> type any .
  data: dym  type table of rssource-line,
        code type table of rssource-line,
        line type edpline,
         prog(8)  type c,
         msg(120type c,
         lin(3)   type c,
         wrd(10)  type c,
         off(3)   type c.


  clear it_return. refresh it_return.


  append 'PROGRAM ZEXECUTE_SQL.'  to code.
  append 'data: l_icon type icon occurs 0 with header line.' to code.
  append 'FORM EXECUTE tables it_return.' to code.
* Dinamico
  split sql at '@' into table dym.
  if not ( dym is initial ).
    loop at dym assigning <l_ref> .
      if ( <l_ref> <> '' ).
        append <l_ref>  to code.
      endif.
    endloop.
  endif.
* Estatico
  append ' APPEND l_icon. ' to code.
  append ' ENDSELECT. ' to code.
  append ' it_return[] = l_icon[].' to code.
  append 'ENDFORM.'  to code.


* Se genera el programa
  generate subroutine pool code name prog
                         message msg
                         line    lin
                         word    wrd
                         offset  off.


  if sy-subrc ne 0.
*    raise error_formula.
  else.
    try.
*     Ejecuto el Form (execute) del programa generado
      perform execute in program (prog)
                      tables it_return.
    endtry.
  endif.
endfunction

Encapsulamiento:
Paso 1.
*&---------------------------------------------------------------------*
*&      Form  execute_query_v2
*&---------------------------------------------------------------------*
form execute_query_v2 tables it_out using sql.


  submit zhr_pa_acu_gensql
  using selection-sets of program 'ZGENSQL'
        with sql eq sql
        exporting list to memory
          and return.
  import it_out from memory id 'RSQL'.
  free memory id 'RSQL'.
endform.                    
Paso 2. 
*&---------------------------------------------------------------------*
*& Report  ZGENSQL
*&
*&---------------------------------------------------------------------*
report  zgensql.
parameterssql type string.
data: it_out type icon occurs 0 with header line.

start-of-selection.
  call function 'Z_EXECUTE_SQL'
    exporting
      sql       = sql
    tables
      it_return = it_out.
  export it_out to memory id 'RSQL'
. 

No hay comentarios:

Publicar un comentario