Dynamisch Interne Tabelle erzeugen

Nützliche Abap Codes und Code Fragmente

Dynamisch Interne Tabelle erzeugen

Postby Tron » Mon Oct 25, 2021 7:57 am

1.) Durch kopieren von Feldern einer Vorlage Struktur 1:1
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. *&---------------------------------------------------------------------*
  2. *&      Form  create Structure dynamically
  3. *&---------------------------------------------------------------------*
  4. *       text
  5. *----------------------------------------------------------------------*
  6. FORM create_itab USING i_tdef TYPE dd02l-tabname .
  7.   <dyn_field> TYPE any,
  8.   <dyn_rec>   TYPE any,
  9.   <dyn_tab>   TYPE ANY TABLE.
  10.  
  11.   DATA:
  12.     l_ref_type_descr_ddic   TYPE REF TO cl_abap_typedescr,
  13.     l_ref_type_descr_line   TYPE REF TO cl_abap_typedescr,
  14.     l_ref_struct_descr_line TYPE REF TO cl_abap_structdescr,
  15.     l_tab_component         TYPE        cl_abap_structdescr=>component_table.
  16.  
  17.   DATA:
  18.     lop_tabledescr    TYPE REF TO cl_abap_tabledescr,
  19.     ldp_dynaic_record TYPE REF TO data,
  20.     ldp_dyn_table     TYPE REF TO data,
  21.     lop_strucdescr    TYPE REF TO cl_abap_structdescr.
  22.  
  23.  
  24.   l_ref_type_descr_ddic = cl_abap_typedescr=>describe_by_name( i_tdef ).
  25.   l_ref_struct_descr_line ?= l_ref_type_descr_ddic.
  26.   l_tab_component = l_ref_struct_descr_line->get_components( ).
  27.  
  28.   lop_strucdescr = cl_abap_structdescr=>create( l_tab_component ).
  29.   CREATE DATA ldp_dynaic_record TYPE HANDLE lop_strucdescr.
  30.  
  31.   lop_tabledescr ?= cl_abap_tabledescr=>create( p_line_type = lop_strucdescr ).
  32.   CREATE DATA ldp_dyn_table TYPE HANDLE lop_tabledescr.
  33.  
  34.   ASSIGN ldp_dynaic_record->* TO <dyn_rec>.
  35.   ASSIGN ldp_dyn_table->* TO <dyn_tab>.
  36.  
  37.  
  38.  
GeSHi ©

2. ) Durch Reduktion der Felder einer Vorlage Struktur
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. DATA: ls_mara TYPE t005t,  " Struktur mit Obermenge der Felder, die in der Zielstruktur vorhanden sein sollen
  2.       lr_data TYPE REF TO data.
  3.  
  4. DATA(lt_components) = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_data( ls_mara ) )->get_components( ).
  5. DELETE lt_components WHERE name <> 'SPRAS' " Nur diese 3 Komponenten erhalten
  6.                        AND name <> 'LAND1'
  7.                        AND name <> 'LANDX'.
  8. DATA(lo_descr_reduced) = cl_abap_structdescr=>create( lt_components ).
  9. CREATE DATA lr_data TYPE HANDLE lo_descr_reduced.
  10. ASSIGN lr_data->* TO FIELD-SYMBOL(<ls_reduced_line>).
  11.  
GeSHi ©


3.) Separate Erzeugung :
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. FORM create_itab2 USING i_tdef TYPE dd02l-tabname .
  2.   DATA t_field_def TYPE TABLE OF rpy_fiel.
  3.   DATA s_field_def TYPE rpy_fiel.
  4.  
  5.   <dyn_field> TYPE any,
  6.   <dyn_rec>   TYPE any,
  7.   <dyn_tab>   TYPE ANY TABLE.
  8.  
  9.  
  10.   UNASSIGN:
  11.     <dyn_field>,
  12.     <dyn_rec>,
  13.     <dyn_tab>.
  14.  
  15.   CALL FUNCTION 'RPY_TABLE_READ'
  16.     EXPORTING
  17.       table_name       = i_tdef
  18.     TABLES
  19.       tabl_fields      = t_field_def
  20.     EXCEPTIONS
  21.       cancelled        = 1
  22.       not_found        = 2
  23.       permission_error = 3
  24.       illegal_type     = 4
  25.       OTHERS           = 5.
  26.  
  27.   IF sy-subrc <> 0.
  28.     EXIT.
  29.   ENDIF.
  30.  
  31.   DATA:
  32.     ldp_dyn_table     TYPE REF TO data,
  33.     lop_tabledescr    TYPE REF TO cl_abap_tabledescr,
  34.     ldp_dynaic_record TYPE REF TO data,
  35.     lop_strucdescr    TYPE REF TO cl_abap_structdescr,
  36.     ls_component      TYPE abap_componentdescr,
  37.     lt_component      TYPE abap_component_tab.
  38.  
  39.   DATA l_field_length   TYPE i.
  40.   DATA l_decimals   TYPE i.
  41.   DATA fields TYPE  rfc_db_fld.
  42.   DATA lv_int1 TYPE int1.
  43.   DATA lv_int2 TYPE int2.
  44.   DATA lv_int4 TYPE int4.
  45.  
  46. *FIELDNAME                      OFFSET LENGTH T FIELDTEXT
  47. *MANDT                          000000 000003 C
  48. *MATNR                          000003 000018 C
  49. *SPRAS                          000021 000002 C
  50. *MAKTX                          000023 000040 C
  51. *MAKTG                          000063 000040 C
  52.  
  53.  
  54.   LOOP AT t_field_def INTO s_field_def.
  55.  
  56.       CASE s_field_def-datatype.
  57.         WHEN 'CHAR' OR 'UNIT' OR 'CUKY' OR 'CLNT'
  58. .         l_field_length = s_field_def-length.
  59.           ls_component-name = s_field_def-fieldname.
  60.           ls_component-type = cl_abap_elemdescr=>get_c( p_length = l_field_length ).
  61.           APPEND ls_component TO lt_component.
  62.  
  63.         WHEN 'LANG'.
  64.           l_field_length = s_field_def-length.
  65.           ls_component-name = s_field_def-fieldname.
  66.           ls_component-type = cl_abap_elemdescr=>get_c( p_length = l_field_length ).
  67.           APPEND ls_component TO lt_component.
  68.  
  69.         WHEN 'DEC' OR 'QUAN' OR 'CURR'.
  70.           l_field_length = s_field_def-intlength
  71. .           l_decimals = s_field_def-decimals.
  72.           ls_component-name = s_field_def-fieldname.
  73.           ls_component-type = cl_abap_elemdescr=>get_p( p_length = l_field_length  p_decimals = l_decimals ).
  74.           APPEND ls_component TO lt_component.
  75.  
  76.         WHEN 'NUMC'.
  77.           l_field_length = s_field_def-length.
  78.           ls_component-name = s_field_def-fieldname.
  79.           ls_component-type = cl_abap_elemdescr=>get_n( p_length = l_field_length ).
  80.           APPEND ls_component TO lt_component.
  81.  
  82.         WHEN 'INT1' OR 'INT2' OR 'INT4'.
  83.  
  84.           ls_component-name = s_field_def-fieldname.
  85.  
  86.           IF s_field_def-datatype = 'INT1'.
  87.             ls_component-type ?= cl_abap_elemdescr=>describe_by_data( p_data = lv_int1 ).
  88.           ELSEIF  s_field_def-datatype = 'INT2'.
  89.             ls_component-type ?= cl_abap_elemdescr=>describe_by_data( p_data = lv_int2 ).
  90.           ELSEIF  s_field_def-datatype = 'INT4'.
  91.             ls_component-type ?= cl_abap_elemdescr=>describe_by_data( p_data = lv_int4 ).
  92.           ENDIF.
  93.  
  94.           APPEND ls_component TO lt_component.
  95.  
  96.         WHEN 'DATS'.
  97.           l_field_length = s_field_def-length.
  98.           ls_component-name = s_field_def-fieldname.
  99.           ls_component-type = cl_abap_elemdescr=>get_d( ).
  100.           APPEND ls_component TO lt_component.
  101.  
  102.         WHEN 'TIMS'.
  103.           l_field_length = s_field_def-length.
  104.           ls_component-name = s_field_def-fieldname.
  105.           ls_component-type = cl_abap_elemdescr=>get_t( ).
  106.           APPEND ls_component TO lt_component.
  107.  
  108.         WHEN OTHERS.
  109.           break-point.
  110.           l_field_length = s_field_def-length.
  111.           ls_component-name = s_field_def-fieldname.
  112.           ls_component-type = cl_abap_elemdescr=>get_c( p_length = l_field_length ).
  113.           APPEND ls_component TO lt_component.
  114.  
  115.       ENDCASE.
  116.  
  117.  
  118.   lop_strucdescr = cl_abap_structdescr=>create( lt_component ).
  119.   CREATE DATA ldp_dynaic_record TYPE HANDLE lop_strucdescr.
  120.   lop_tabledescr ?= cl_abap_tabledescr=>create( p_line_type = lop_strucdescr ).
  121.  
  122.   CREATE DATA ldp_dyn_table TYPE HANDLE lop_tabledescr.
  123.  
  124.   ASSIGN ldp_dynaic_record->* TO <dyn_rec>.
  125.   ASSIGN ldp_dyn_table->* TO <dyn_tab>.
  126.  
GeSHi ©


Einen konkreten Anwendungsfall gibt es z.B. HIER

gruß Jens
Tron
.....
.....
 
Posts: 1107
Joined: Sat Aug 04, 2007 10:21 pm

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest

cron