Temat: ABAP - pomoc
REPORT zstct.
TYPES:
BEGIN OF val_ref,
name TYPE string,
value TYPE REF TO data,
END OF val_ref,
BEGIN OF val_string,
name TYPE string,
value TYPE string,
END OF val_string,
val_tab_s TYPE TABLE OF val_string,
val_tab_r TYPE TABLE OF val_ref.
DATA:
_val_string TYPE val_string,
_val_tab_s TYPE val_tab_s,
_val_ref TYPE val_ref,
_val_tab_r TYPE val_tab_r.
*----------------------------------------------------------------------*
* CLASS zgettab DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zgettab DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
gettab IMPORTING dbtab TYPE csequence byref TYPE boolean_flg EXPORTING val_tab TYPE ANY TABLE.
ENDCLASS. "zgettab DEFINITION
*----------------------------------------------------------------------*
* CLASS zgettab IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zgettab IMPLEMENTATION.
METHOD gettab.
DATA:
_type_desc TYPE REF TO cl_abap_typedescr,
_struct_desc TYPE REF TO cl_abap_structdescr,
_table_desc TYPE REF TO cl_abap_tabledescr,
_comp_desc TYPE abap_compdescr,
_table_ref TYPE REF TO data,
_struct_ref TYPE REF TO data,
_components TYPE cl_abap_structdescr=>component_table,
_component LIKE LINE OF _components,
_error TYPE REF TO cx_root,
_type TYPE c,
_comps TYPE i.
FIELD-SYMBOLS: <table> TYPE ANY TABLE,
<struct> TYPE ANY,
<c> TYPE ANY.
cl_abap_typedescr=>describe_by_name(
EXPORTING p_name = dbtab
RECEIVING p_descr_ref = _type_desc
EXCEPTIONS type_not_found = 4 ).
IF sy-subrc = 4.
MESSAGE 'Type not found' TYPE 'E'.
ENDIF.
TRY.
_struct_desc ?= _type_desc.
_components = _struct_desc->get_components( ).
_struct_desc = cl_abap_structdescr=>create( _components ).
_table_desc = cl_abap_tabledescr=>create( _struct_desc ).
CREATE DATA _table_ref TYPE HANDLE _table_desc.
ASSIGN _table_ref->* TO <table>.
SELECT * FROM (dbtab) INTO CORRESPONDING FIELDS OF TABLE <table>.
LOOP AT <table> ASSIGNING <struct>.
DESCRIBE FIELD <struct> TYPE _type COMPONENTS _comps.
DO _comps TIMES.
READ TABLE _struct_desc->components INTO _comp_desc INDEX sy-index.
IF byref EQ ' '.
MOVE _comp_desc-name TO _val_string-name.
ASSIGN COMPONENT sy-index OF STRUCTURE <struct> TO <c>.
MOVE <c> TO _val_string-value.
APPEND _val_string TO _val_tab_s.
CLEAR _val_string.
ENDIF.
IF byref EQ 'X'.
MOVE _comp_desc-name TO _val_ref-name.
ASSIGN COMPONENT sy-index OF STRUCTURE <struct> TO <c>.
GET REFERENCE OF <c> INTO _val_ref-value.
APPEND _val_ref TO _val_tab_r.
CLEAR _val_ref.
ENDIF.
ENDDO.
ENDLOOP.
CATCH cx_sy_move_cast_error INTO _error.
MESSAGE _error TYPE 'E'.
CATCH cx_sy_struct_creation INTO _error.
MESSAGE _error TYPE 'E'.
ENDTRY.
ENDMETHOD. "gettab
ENDCLASS. "zgettab IMPLEMENTATION
START-OF-SELECTION.
zgettab=>gettab(
EXPORTING dbtab = 'SFLIGHT'
byref = 'X'
"lub
"byref = ' '
"RECEIVING = tabela z wynikami
).
W dbtab podajesz nazwę tabeli.
Po wykonaniu dostajesz tabelę z dwoma kolumnami name i value,
wypełnioną wszystkimi danymi z tabeli bazy danych.
Dla byref ' ' wartość (value) danego pola z bazy danych będzie skonwertowana do stringa, dla 'X' będzie trzymana jako referencja, a więc typ zostanie zachowany.