‎2005 Dec 14 7:17 PM
Hi,
I have a requirement to select the data from a table dynamically. Only during run time I will know the structure of that table & table name. In this case can anyone give any tips/sample code how it can be accomplished. Help appreciated.
Thanks,
Abhi
‎2005 Dec 14 10:06 PM
Hi Abhi,
You can try this program. This program approach is similar somewhat but it works for MARA too.
report zsharad.
type-pools : abap.
data: w_line type ref to data.
data : it_details type abap_compdescr_tab,
wa_details type abap_compdescr.
data : ref_descr type ref to cl_abap_structdescr.
field-symbols : <fs> type any,
<fval> type any,
<fnam> type any.
parameters: p_table(30) type c.
start-of-selection.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].
create data w_line type (p_table).
assign w_line->* to <fs>.
select * from mara into <fs>
where matkl = '01'.
do.
assign component sy-index of structure <fs> to <fval>.
if sy-subrc <> 0.
exit.
endif.
read table it_details into wa_details
index sy-index.
write :/01 wa_details-name,
<fval>.
free <fval>.
enddo.
endselect.
‎2005 Dec 14 7:19 PM
Sure, check out this sample program. It is a dynamic table read.
report zrich_0002.
type-pools: slis.
field-symbols: <dyn_table> type standard table,
<dyn_wa>,
<dyn_field>.
data: alv_fldcat type slis_t_fieldcat_alv,
it_fldcat type lvc_t_fcat.
type-pools : abap.
data : it_details type abap_compdescr_tab,
wa_details type abap_compdescr.
data : ref_descr type ref to cl_abap_structdescr.
data: new_table type ref to data,
new_line type ref to data,
wa_it_fldcat type lvc_s_fcat.
selection-screen begin of block b1 with frame title text .
parameters: p_table(30) type c.
selection-screen end of block b1.
* Get the structure of the table.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].
loop at it_details into wa_details.
clear wa_it_fldcat.
wa_it_fldcat-fieldname = wa_details-name .
wa_it_fldcat-datatype = wa_details-type_kind.
wa_it_fldcat-inttype = wa_details-type_kind.
wa_it_fldcat-intlen = wa_details-length.
wa_it_fldcat-decimals = wa_details-decimals.
append wa_it_fldcat to it_fldcat .
endloop.
* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = it_fldcat
importing
ep_table = new_table.
assign new_table->* to <dyn_table>.
* Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.
* Select Data from table.
select * into table <dyn_table>
from (p_table).
* Write out data from table.
loop at <dyn_table> into <dyn_wa>.
do.
assign component sy-index of structure <dyn_wa> to <dyn_field>.
if sy-subrc <> 0.
exit.
endif.
if sy-index = 1.
write:/ <dyn_field>.
else.
write: <dyn_field>.
endif.
enddo.
endloop.
Welcome to SDN. Please remember to award points for helpful answers and mark you post as solved when solved completely. Thanks.
Regards,
Rich Heilman
Fixed Code
Message was edited by: Rich Heilman
‎2005 Dec 14 8:07 PM
Hi Rich,
I am thankful & really appreciate your help, However I am getting some errors, when I used your code I am getting dump at the select statement with the error 'SAPSQL_SELECT_TAB_TOO_SMALL' i.e With ABAP/4 Open SQL array select, the output table is too small.
I will try to resolve this & keep you posted but in case if your are already aware of this problem, can you please suggest to get rid of it.
Thanks again for your help & time.
regards.Abhi
‎2005 Dec 14 8:13 PM
‎2005 Dec 14 8:20 PM
‎2012 Jan 28 11:47 AM
Use this code instead of previouse:
* Dynamic table and structure declaration
DATA:
lv_db_table_name TYPE string,
dy_table TYPE REF TO data.
FIELD-SYMBOLS:
<lt_data> TYPE STANDARD TABLE.
* Prepare
lv_db_table_name = 'RSZCALC'.
TRANSLATE lv_db_table_name TO UPPER CASE.
*---------------------------------------------------------------------
* Create dynamical table based on existing one
*---------------------------------------------------------------------
CREATE DATA dy_table TYPE TABLE OF (lv_db_table_name).
ASSIGN dy_table->* TO <lt_data>.
*---------------------------------------------------------------------
* Select content of a table
*---------------------------------------------------------------------
REFRESH <lt_data>.
SELECT * FROM (lv_db_table_name) INTO TABLE <lt_data>.
Regards Pavel.
‎2005 Dec 14 8:34 PM
‎2005 Dec 14 9:02 PM
‎2005 Dec 14 9:28 PM
Its weird, its like the client field is shifting everything to the left. I don't understand it.
Anyway, the fix for the types, is as follows.
loop at it_details into wa_details.
clear wa_it_fldcat.
wa_it_fldcat-fieldname = wa_details-name .
wa_it_fldcat-datatype = wa_details-type_kind.
<b> wa_it_fldcat-inttype = wa_details-type_kind.</b>
wa_it_fldcat-intlen = wa_details-length.
wa_it_fldcat-decimals = wa_details-decimals.
append wa_it_fldcat to it_fldcat .
endloop.
Regards,
Rich Heilman
‎2005 Dec 14 11:14 PM
‎2005 Dec 15 7:42 PM
‎2005 Dec 14 10:06 PM
Hi Abhi,
You can try this program. This program approach is similar somewhat but it works for MARA too.
report zsharad.
type-pools : abap.
data: w_line type ref to data.
data : it_details type abap_compdescr_tab,
wa_details type abap_compdescr.
data : ref_descr type ref to cl_abap_structdescr.
field-symbols : <fs> type any,
<fval> type any,
<fnam> type any.
parameters: p_table(30) type c.
start-of-selection.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].
create data w_line type (p_table).
assign w_line->* to <fs>.
select * from mara into <fs>
where matkl = '01'.
do.
assign component sy-index of structure <fs> to <fval>.
if sy-subrc <> 0.
exit.
endif.
read table it_details into wa_details
index sy-index.
write :/01 wa_details-name,
<fval>.
free <fval>.
enddo.
endselect.