‎2006 Mar 14 1:47 AM
Hi,
I'm trying to create an internal table dynamically as i would be able to determine the structure of the table based on the user input.
I've used the sample code from this forum ...
REPORT ZRICH_0003 .
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>.
<b>* Select Data from table.
select * into table <dyn_table> from
(p_table).</b>
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.
I'm able to get the structure of the table that i want, but when i'm trying to select data from the table into this internal table..as highlighted in the sample code above..i'm getting a short dump...saying that ' the database table is 600 bytes wide but the internal table is only 576 bytes wide.
The internal table is declared as
field-symbols: <dyn_table> type standard table..
Could anyone please tell me how to rectify this.
Thanks in advance,
Harsha.
‎2006 Mar 14 2:12 AM
‎2006 Mar 14 2:42 AM
Yes, I have actually had the same problem with that example program. It is not 100% effective. I have not yet figured out what the exact problem is when dealing with certain tables. I wrote that program using the T001 table as a test and it works good, but some tables do not work so well. It may have something to do with the types of fields in the table.
What release are you on? In newer releases, there is something called Run Time Type Services (RTTS) which may be a better way to go for you.
Regards,
Rich Heilman
‎2006 Mar 14 3:00 AM
Hi Rich,
Thanks for replying.
I'm on 4.6C. Could you please describe any other way to read the table entries once the internal table is build.
Thanks,
Harsha
‎2006 Mar 14 3:15 AM
Check the comments section from my weblog mentioned above. It seems that Rene has a viable solution, although I haven't tried it and I thought that I remember trying to use INTO CORRESPONDING FIELDS OF TABLE, but I think that it caused other problems. Its forth a shot, give it a try.
Regards,
Rich Heilman
‎2006 Mar 14 3:47 AM
Hi,
We had faced such a problem too.We had been missing out on one of the fields when we declared a structure to hold the values of the table.
Usually, every database table has a mandt field which by default is part of the primary key.
Are you sure you have included such a field in the structure or internal table you have created?
Regards,
Smitha
‎2006 Mar 14 4:05 AM
Hi Smitha,
I'm building the internal table by getting the structure using the method
cl_abap_typedescr=>describe_by_name( p_table ).
where p_table is the table name determined dynamically..
Now using this structure, i'm building an internal table by calling the method
call method cl_alv_table_create=>create_dynamic_table
I've checked all the fields after the internal table has been created .. and it contains all the fields of the table that i'm supplying initially..
But when i read data into that internal table, it gives me that dump..I've described it in this post earlier.
Any more suggestions would be very helpful.
Thanks,
Harsha