Creation of dynamic internal table in ABAP
TYPES : BEGIN OF ty_prn, ” This is a structure having three columns or pernr, suby and objps pernr TYPE persno, subty TYPE subty, objps TYPE objps,
END OF ty_prn.
FIELD-SYMBOLS : <f1> TYPE any,
<f2> type TABLE .
data: dy_table type ref to data,
dy_line type ref to data,
xfc type lvc_s_fcat,
ifc type lvc_t_fcat.
data : idetails type abap_compdescr_tab,
xdetails type abap_compdescr.
data : ref_table_des type ref to cl_abap_structdescr.
DATA : prn TYPE STANDARD TABLE OF ty_prn, ” table and workarea of this type wa LIKE LINE OF prn.
data : str TYPE STring.CONCATENATE ‘PA’ infotype INTO str. ” concatenating PA as database table in HR ABAP have naming convention PAnnnnref_table_des ?= cl_abap_typedescr=>describe_by_name( str ). ” getting the structure of the table name dynamically idetails[] = ref_table_des->components[]. ” reading the columns name dynamicaly
loop at idetails into xdetails. ” loop at the columns name and passing these in a field catalogue clear xfc. xfc-fieldname = xdetails-name . xfc-datatype = xdetails-type_kind. xfc-inttype = xdetails-type_kind. xfc-intlen = xdetails-length. xfc-decimals = xdetails-decimals. append xfc to ifc. endloop.
call method cl_alv_table_create=>create_dynamic_table ” exporting it_fieldcatalog = ifc importing ep_table = dy_table. assign dy_table->* to <f2>. ” now we can directly select from table into this <f2> as any normal internal table.
Hello Sahai,
Thanks for your example. I have never done HR programming by myself. But I want to make an other suggestion for a dynamic table linked to dynamic SQL.
I think there are better way, with much more performace (I'm sure there are many), but this minimal example is very easy.
*----------- ALV LIST
TYPES:
BEGIN OF ty_layout,
repid TYPE syrepid,
restrict TYPE salv_de_layout_restriction,
default TYPE sap_bool,
layout TYPE disvariant-variant,
END OF ty_layout.
DATA:
go_alv TYPE REF TO cl_salv_table,
go_functions TYPE REF TO cl_salv_functions_list,
go_layout TYPE REF TO cl_salv_layout,
gs_key TYPE salv_s_layout_key.
DATA: gs_layout TYPE ty_layout.
*------------ END ALV LIST
DATA: dref TYPE REF TO data,
"generic Data object
lv_typeoftable TYPE dd02l-tabname,
"generic table
where_tab TYPE TABLE OF edpline,
"itab for the WHERE option
where_line TYPE edpline.
" Working area
FIELD-SYMBOLS: <table> TYPE ANY TABLE. "table which will be assigne dynamicly
lv_typeoftable = 'MARA'.
"Here you can choose the table you want to use
where_line = 'matnr = ''000000000001000439'''.
"Write a real meaningfull Where Option
APPEND where_line TO where_tab.
where_line = ' AND '.
APPEND where_line TO where_tab.
where_line = 'bismt NE '''' '.
APPEND where_line TO where_tab.
CREATE DATA dref TYPE TABLE OF (lv_typeoftable).
" Creating a pointer to a dataobject of the type table of mara
ASSIGN dref->* TO <table>.
"you cant accesse pointers so you will need a field symbol
"Now you can do a dynamic select statment into a dynamic assigned table
SELECT *
FROM (lv_typeoftable)
INTO TABLE <table>
WHERE (where_tab)
.
" Build ALV - that should be clear.
IF sy-subrc <> 0.
cl_salv_table=>factory(
IMPORTING
r_salv_table = go_alv
CHANGING
t_table = <table> ).
gs_layout-repid = sy-repid.
gs_layout-default = 'X'.
gs_layout-layout = '/STANDARD'.
gs_key-report = sy-repid.
go_layout = go_alv->get_layout( ).
go_layout->set_key( gs_key ).
gs_layout-restrict = if_salv_c_layout=>restrict_none.
go_layout->set_default( gs_layout-default ).
go_layout->set_save_restriction( gs_layout-restrict ).
go_layout->set_initial_layout( gs_layout-layout ).
go_functions = go_alv->get_functions( ).
go_functions->set_all( ).
go_alv->display( ).
ENDIF.
Yes that way you can create a dynamic table
if you have a big structure to create
Also you can copy the base structures and append to that as well
A sample here too:
code snippet:
method test_create_components.
data: lt_components type cl_abap_structdescr=>component_table
,lv_field_name type string
,lv_field_number type string
.
field-symbols: <lfs_components> like line of lt_components
.
* break-point.
do 5 times.
append initial line to lt_components assigning <lfs_components>.
clear: lv_field_name.
lv_field_number = sy-index.
condense lv_field_number.
concatenate 'FIELD0' lv_field_number into lv_field_name.
*& Name for Each Field
<lfs_components>-name = lv_field_name.
*& For This Example we will build all field with type C and lenght 1024
<lfs_components>-type ?= cl_abap_typedescr=>describe_by_name(
'BUKRS' ).
enddo.
data: lt_dref_table type ref to data,
ls_dref_line type ref to data
.
field-symbols: <lfs_table> type any table
,<lfs_line> type any
.
zcl_dynamic_out_helper=>create_dynamic_table_from_comp(
exporting
it_components = lt_components
importing
rt_dref_table = lt_dref_table
rs_dref_line = ls_dref_line
).
assign lt_dref_table->* to <lfs_table>.
assign ls_dref_line->* to <lfs_line>.
endmethod. "test_create_components
Thanks ... yes there are many other approach to this... I shared this coz its tough to remember the fundamentals when doing dynamic coding.. 😎