Difference between revisions of "SAP ABAP DYNAMIC"

From SapWiki
Line 1: Line 1:
 
==Ejemplo 01: generar archivo CSV independiente del tipo==
 
==Ejemplo 01: generar archivo CSV independiente del tipo==
<nowiki>FORM crear_csv USING p_tabla TYPE ANY TABLE
+
<nowiki>FORM crear_csv USING p_tabla TYPE ANY TABLE
 
             CHANGING p_tabla_csv TYPE ppe_cust_table.
 
             CHANGING p_tabla_csv TYPE ppe_cust_table.
  

Revision as of 19:26, 11 April 2020

Ejemplo 01: generar archivo CSV independiente del tipo

FORM crear_csv USING p_tabla TYPE ANY TABLE
            CHANGING p_tabla_csv TYPE ppe_cust_table.

  CONSTANTS: c_field_separator TYPE c VALUE ';',
             c_darl_number(12) TYPE c VALUE '1234567890 '.

  DATA: cl_descr_struc_ref TYPE REF TO cl_abap_structdescr,
        it_components      TYPE abap_compdescr_tab,
        wa_components      LIKE LINE OF it_components.

  DATA lt_data TYPE ppe_cust_table.

  DATA: l_struc_index     TYPE syindex,
        l_len_string      TYPE i,
        l_max_field       TYPE char1024,
        l_struc_raw_data  TYPE char1024,
        l_start_string    TYPE i,
        l_len_field_sep   TYPE i,
        l_date_extern(30) TYPE c,
        l_help_id         LIKE tline-tdline,
        l_tabix           TYPE sytabix.

  FIELD-SYMBOLS <fs_field> TYPE ANY.
  FIELD-SYMBOLS <ls_envio> TYPE ANY.

  l_len_field_sep = STRLEN( c_field_separator ).

  LOOP AT p_tabla ASSIGNING <ls_envio>.
    CLEAR: l_struc_index, l_len_string, l_struc_raw_data, l_start_string.

    IF it_components[] IS INITIAL.
      cl_descr_struc_ref ?= cl_abap_typedescr=>describe_by_data( <ls_envio> ).
* obtener campos de estructura
      it_components = cl_descr_struc_ref->components.
    ENDIF.

    DO.
      ADD 1 TO l_struc_index.
      CLEAR l_len_string.
      READ TABLE it_components INTO wa_components INDEX sy-index.
      IF sy-subrc = 0.
        ASSIGN COMPONENT wa_components-name OF STRUCTURE <ls_envio> TO <fs_field>.

        CASE wa_components-type_kind .
          WHEN 'P'.
            WRITE <fs_field> TO l_date_extern.
            CATCH SYSTEM-EXCEPTIONS conversion_errors  = 4.
              IF sy-subrc <> '0'.
*                MESSAGE e899(ux) WITH l_type <f_source> l_help_id
*                                      l_tabix RAISING conversion_failed.
              ENDIF.
            ENDCATCH.
            CONDENSE l_date_extern.
            ASSIGN l_date_extern TO <fs_field>.
          WHEN 'D'.
            MOVE <fs_field> TO l_date_extern.
*20191220 -> 20/12/2019
            REPLACE FIRST OCCURRENCE OF REGEX '(\d{4})(\d{2})(\d{2})' IN l_date_extern
                                           WITH '$3/$2/$1'.
        ENDCASE.

      ELSE.
        EXIT.
      ENDIF.

      IF l_struc_index <> 1.
        l_max_field+l_len_string(l_len_field_sep) = c_field_separator.
        l_len_string = l_len_field_sep.
      ENDIF.

      l_max_field+l_len_string = <fs_field>.
      l_len_string = STRLEN( l_max_field ).
      CHECK l_len_string <> '0'.
      l_struc_raw_data+l_start_string(l_len_string) = l_max_field.
      l_start_string = l_start_string + l_len_string.
    ENDDO.

    APPEND l_struc_raw_data TO lt_data.

  ENDLOOP.

  p_tabla_csv[] = lt_data[].
ENDFORM.                    "crear_csv

Ejemplo 02: leer archivo desde servidor separado por tabs, independiente del tipo

 types: ty_t001 type t001.
  DATA lt_t001 TYPE STANDARD TABLE OF ty_t001.

  PERFORM open_un USING '/tmp/t001_dat2.txt' CHANGING lt_t001.

FORM open_un USING p_file
          CHANGING p_table TYPE ANY TABLE.
  DATA: lr_structdescr TYPE REF TO cl_abap_structdescr,
        lr_datadescr   TYPE REF TO cl_abap_datadescr,
        lr_tabledescr  TYPE REF TO cl_abap_tabledescr,
        line_descr TYPE REF TO cl_abap_typedescr.
  DATA: l_string  TYPE string,
        l_string2 TYPE string,
        l_campo   TYPE string,
        ld_name TYPE string.
  FIELD-SYMBOLS: <fs> TYPE any,
                 <wa> TYPE any.
  DATA: wa_ref TYPE REF TO data.

  lr_tabledescr ?= cl_abap_tabledescr=>describe_by_data( p_table ).
  line_descr = lr_tabledescr->get_table_line_type( ).
  ld_name = line_descr->get_relative_name( ).
*     get the field list of the table linetype
  DATA(lt_components) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_name( ld_name ) )->components.

  CREATE DATA wa_ref TYPE (ld_name).   "Suitable work area
  ASSIGN wa_ref->* TO <wa>.

  OPEN DATASET p_file FOR INPUT IN TEXT MODE
                            ENCODING DEFAULT
                            WITH SMART LINEFEED.
  IF sy-subrc <> 0.
    RETURN.
  ENDIF.

  WHILE sy-subrc = 0.
    READ DATASET p_file INTO l_string.
    CHECK l_string IS NOT INITIAL.
    DATA(l_index) = 1.
    WHILE l_string IS NOT INITIAL.
      SPLIT l_string AT cl_abap_char_utilities=>horizontal_tab INTO l_string2 l_string.
      READ TABLE lt_components INTO data(ls_comp) INDEX l_index.
      IF sy-subrc = 0.
        ADD 1 TO l_index.
        CONCATENATE '<WA>' ls_comp-name INTO l_campo SEPARATED BY '-'.
        ASSIGN (l_campo) TO <fs>.
        MOVE l_string2 TO <fs> .
      ELSE.
        EXIT.
      ENDIF.
    ENDWHILE.
    INSERT <wa> INTO TABLE p_table.
  ENDWHILE.

ENDFORM.