0% found this document useful (0 votes)
11 views

Zananias Dynamic Table

The document describes a report that dynamically generates an ABAP table from a database table based on the table and field metadata. It retrieves the table and field metadata, builds an ABAP data structure and table to match it, fills it with data from the database table, and downloads it as a file.

Uploaded by

Rangabashyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Zananias Dynamic Table

The document describes a report that dynamically generates an ABAP table from a database table based on the table and field metadata. It retrieves the table and field metadata, builds an ABAP data structure and table to match it, fills it with data from the database table, and downloads it as a file.

Uploaded by

Rangabashyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

*&---------------------------------------------------------------------*

*& Report ZANANIAS_DYNAMIC_TABLE


*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zananias_dynamic_table.

TYPES:
*Feldtyp
BEGIN OF t_fld.
INCLUDE STRUCTURE dd03p AS dd03p.
TYPES:
mark LIKE rsdxx-mark,
type_icon TYPE dd02d-datatype,
shlporigin_text LIKE dd07t-ddtext,
fkexi(1),
mod(1),
actf(1),
f_display,
switch_id,
END OF t_fld.

DATA: gt_fld_copy TYPE TABLE OF t_fld,


ls_fld_copy TYPE t_fld,
gt_dd03p TYPE TABLE OF dd03p.
DATA:
get_state TYPE dctablget,
got_state TYPE dctablget,
got_state_d TYPE dctablget.

PARAMETERS: p_tab TYPE tabname OBLIGATORY,


p_file TYPE localfile OBLIGATORY.

TYPES: BEGIN OF ty_str_hdr,


field_name TYPE char20,
END OF ty_str_hdr.

DATA: lv_file TYPE string,


lit_hdr TYPE STANDARD TABLE OF ty_str_hdr.
*--------------------------------------------------
DATA: field_list TYPE string,
p_struct_result TYPE REF TO cl_abap_structdescr,
p_table_result TYPE REF TO cl_abap_tabledescr,
p_components TYPE STANDARD TABLE OF abap_componentdescr
WITH KEY name,
ls_components LIKE LINE OF p_components.

DATA: gtt_table TYPE REF TO data,


gs_struct TYPE REF TO data.

FIELD-SYMBOLS: <itab> TYPE INDEX TABLE,


<ls_struct> TYPE any.
*--------------------------------------------

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.


CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = 'p_file'
IMPORTING
file_name = p_file.

START-OF-SELECTION.

get_state-tabd = get_state-tabt = 'A'.

CALL FUNCTION 'DD_TABL_GET'


EXPORTING
get_state = get_state
langu = sy-langu
prid = -2
tabl_name = p_tab
withtext = 'X'
add_typeinfo = 'X'
TABLES
dd03p_tab_a = gt_dd03p
EXCEPTIONS
OTHERS = 2.
gt_fld_copy = gt_dd03p.

CALL FUNCTION 'DD_LIST_TABFIELDS'


EXPORTING
eutype = 'V'
p_with_type = 'X'
TABLES
fieldtab = gt_fld_copy
EXCEPTIONS
not_executed = 1
OTHERS = 2.

LOOP AT gt_fld_copy INTO DATA(wa_fields) WHERE mark IS NOT INITIAL.

CONCATENATE wa_fields-fieldname field_list INTO field_list SEPARATED BY space.

ls_components-name = wa_fields-fieldname.
ls_components-type ?= cl_abap_elemdescr=>describe_by_name( wa_fields-rollname )
.
APPEND ls_components TO p_components.
ENDLOOP.

TRY .
p_struct_result = cl_abap_structdescr=>create( p_components = p_components ).
p_table_result = cl_abap_tabledescr=>create( p_line_type =
p_struct_result ).
CATCH cx_sy_struct_creation. "
CATCH cx_sy_table_creation.

ENDTRY.

END-OF-SELECTION.
CREATE DATA: gtt_table TYPE HANDLE p_table_result,
gs_struct TYPE HANDLE p_struct_result.
ASSIGN gtt_table->* TO <itab>.
ASSIGN gs_struct->* TO <ls_struct>.

IF <itab> IS ASSIGNED.
SELECT (field_list) FROM (p_tab) INTO CORRESPONDING FIELDS OF TABLE <itab>.
ENDIF.

lv_file = p_file.

CALL METHOD cl_gui_frontend_services=>gui_download


EXPORTING
filename = lv_file
filetype = 'ASC'
write_field_separator = 'X'
fieldnames = lit_hdr
CHANGING
data_tab = <itab>
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24.
IF sy-subrc <> 0.
WRITE:/ 'successfully dowloaded' COLOR COL_POSITIVE.
ENDIF.

You might also like