SAP ABAP DYNAMIC

From SapWiki
Revision as of 19:18, 11 April 2020 by WikiSysop (talk | contribs) (Created page with "==EJemplo 01: leer archivo desde servidor separado por tabs, independiente del tipo== <nowiki>types: ty_t001 type t001. DATA lt_t001 TYPE STANDARD TABLE OF ty_t001. PER...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

EJemplo 01: 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.