Dynamically get structure of a dynamic table - abap

I want to dynamically get the structure of a dynamic table. Getting the table is no problem, but I stuck with getting the structure of the table.
DATA: lo_dynamic_table TYPE REF TO data.
FIELD-SYMBOLS: <lt_table_structure> TYPE table,
<ls_table_structure> TYPE any.
CREATE DATA lo_dynamic_table TYPE TABLE OF (lv_my_string_of_table).
ASSIGN lo_dynamic_table->* TO <lt_table_structure>.
// some code assigning the structure
Now I want to execute this command:
SELECT SINGLE * FROM (lv_my_string_of_table) INTO <ls_table_structure> WHERE (lv_dynamid_where_field).
If there is any other solution, I will be okay with that.

Take use of RTTS.
Runtime type services
With this framework You are able to get the desired type during runtime.
http://wiki.scn.sap.com/wiki/pages/viewpage.action?pageId=42965
The desired class should be CL_ABAP_TABLEDESCR or even CL_ABAP_DATADESCR.
They will do the work for You.
As it seems like, You are getting a ddic table name and want to select dynamically data from the tablename into a generic internal table.
So, if You are getting already an nice ddic name, then the usage of rtts is even more simple.
Because You have the ddic name.
Usually there are also many function modules ( mostly in the namespace-prefix "RPY_*" ).
There You surely can find one, which determines the structure of a table, whether it contains includes, and so on.
But, try typedescriptors first, I would start with cl_abap_tabledescr=>get_table_line_type.

This code worked for my case:
DATA: table_name type string,
lo_dynamic_table TYPE REF TO data,
lo_dynamic_line TYPE REF TO data.
FIELD-SYMBOLS: <lt_table_structure> TYPE table,
<ls_table_structure> TYPE any.
table_name = 'yourtablename'.
CREATE DATA lo_dynamic_table TYPE TABLE OF (table_name).
ASSIGN lo_dynamic_table->* TO <lt_table_structure>.
CREATE DATA lo_dynamic_line LIKE LINE OF <lt_table_structure>.
ASSIGN lo_dynamic_line->* TO <ls_table_structure>.

I added code for dynamic hashed table with dynamic keys.
DATA TAB_NAME LIKE SY-TNAME VALUE 'SCARR'.
DATA KEYTAB TYPE TABLE OF STRING.
DATA DREF TYPE REF TO DATA.
FIELD-SYMBOLS <F_TAB> TYPE ANY TABLE.
APPEND 'CARRID' TO KEYTAB.
CREATE DATA dref TYPE HASHED TABLE OF (TAB_NAME)
WITH UNIQUE KEY (KEYTAB).
ASSIGN dref->* TO <F_TAB>.
SELECT *
FROM (TAB_NAME)
INTO TABLE <F_TAB>.
cl_demo_output=>display( <F_TAB> ).
Consider also links.
https://help.sap.com/doc/saphelp_nw70/7.0.31/en-US/79/c55497b3dc11d5993800508b6b8b11/content.htm?no_cache=true
https://archive.sap.com/discussions/thread/92739

Related

How can I extract 'Memory Use Statistics' from ST03N?

I want to select following data from ST03N in a report:
After a performance trace, I noticed that the data might be stored in one of the tables:
MONI
SWNCMONI
I do not exactly know how to extract the CLUSTD data from the table.
I heard of using function module: SWNC_COLLECTOR_GET_AGGREGATES , but the data does not exactly match with the data from ST03N.
As one probably knows, the MONI and the newer SWNCMONI database tables are cluster tables and shouldn't be read directly, use new FM SWNC_COLLECTOR_GET_AGGREGATES for that.
Nevertheless, if you still want this:
TYPES: tt_memory TYPE TABLE OF swncaggmemory.
DATA: ms_monikey TYPE swncmonikey,
dummy TYPE tt_memory.
FIELD-SYMBOLS: <tab> TYPE ANY TABLE.
ASSIGN dummy TO <tab>.
ms_monikey-component = <instance_id>.
ms_monikey-comptype = 'NW Workload'.
ms_monikey-assigndsys = <host>.
ms_monikey-periodtype = 'D'.
ms_monikey-periodstrt = '20200713'.
IMPORT datatable TO <tab>
FROM DATABASE swncmoni(wj) ID ms_monikey
IGNORING STRUCTURE BOUNDARIES.
As you can see that data for PFCG differs from ST03n in spite it is called for the same date.
Answering on your second question: why it differ?
It may depends on data aggregation setting for memory profile
also try to play with aggregation period. Actually I also wasn't able to find correspondence between them.
Many useful info about ST03 is here
https://blogs.sap.com/2007/03/16/how-to-read-st03n-datasets-from-db/

Smartform error "Flat types may only be referenced using LIKE for table parameters"

I created a table ZPDETAIL01 in se11 and activated it.
In smartforms' Form Interface I create a table parameter zdetail in the tables tab, with type assignment as TYPE and associated type as ZPDETAIL01.
When I check it ,an error occurred,"ZPDETAIL01 Flat types may only be referenced using LIKE for table parameters"
Is this error of my table itself or my parameter setting? Thx.
I changed the type assignment to LIKE and problem solved. But I wonder why in the search help button I can't find the LIKE option, only TYPE and TYPE REF.
Simply a flaw in the UI. If it's allowed then it should be listed as a possible value.
But I guess SAP just didn't care to correct those little things of this obsolete technology (i.e. Smart Forms... now prefer Adobe forms or third-party solutions). Note that the list of values come from the table RSFBTYPEIN, and probably LIKE was previously defined in this table but as LIKE became obsolete for typing import and export parameters in function modules, SAP probably removed it: majority wins over minority. Just a guess.
If you wish, you may open a ticket at SAP support to make it corrected.
Behaviors in ABAP 7.52 SP01 (tests done with DDIC objects: flat table SCARR, non-flat table SOTR_TEXTU, table type BAPIRETTAB):
Typing Associated type Button Error message
------ ---------------- -------------- -------------------------------------------
TYPE Flat struc/table Check SCARR Flat types may only be referenced
using LIKE for table parameters
TYPE Flat struc/table Activate Only table types may be used as the
reference type for a table parameter
TYPE Non-flat str/tab. Check/Activate Only table types may be used as the
reference type for a table parameter
TYPE Table type Check/Activate None
LIKE Flat struc/table Check/Activate None
LIKE Non-flat str/tab. Check/Activate None but short dump at runtime (because of
syntax error in FM: "&1" must be a flat
structure. Internal tables, strings, references,
and structures cannot be used as components.)
LIKE Table type Check Type BAPIRETTAB is not allowed in this context
LIKE Table type Activate Tables using LIKE may only reference flat structures
As you see, there's a bigger problem than just not displaying LIKE, there's a short dump in one case!
Note that I didn't test TYPE REF TO, but I doubt a TABLES parameter can use it.

How to list all tables in ABAP programmatically?

All tables may be listed with t-code SE16 and table DD02L. But how can that list be accessed programmatically?
It is rarely a good idea to access the database tables directly since you will have to deal with all kinds of technicalities you probably don't even know about - active / inactive versions, for example. You will also bypass all security and authorization checks, which might be irrelevant to you personally, but is undesirable in general. To get a list of tables, you can use the function module RPY_TABLE_SELECT. This function module will take care of the version handling and provide the description in the language of your choice as well.
Improved Alex code in some way and put it as an option:
SELECT tabname
FROM DD02L
INTO TABLE #DATA(itab)
WHERE TABCLASS = 'TRANSP'.
LOOP AT itab ASSIGNING FIELD-SYMBOL(<FS>).
WRITE:/ <FS>.
ENDLOOP.
Several things were refined: incline declarations were utilized, field-symbols added, SELECT * and WHERE IN were omitted and so on. Also tables in SAP have only TRANSP class, INTTAB class belongs to structures.
Note: the sample is functional since ABAP 7.40, SP08.
An ongoing search resulted in the following snippet:
DATA ITAB TYPE TABLE OF DD02L.
SELECT * FROM DD02L INTO TABLE ITAB WHERE TABCLASS IN ('TRANSP', 'INTTAB').
WRITE :SY-SUBRC .
DATA FS TYPE DD02L.
LOOP AT ITAB INTO FS.
WRITE:/ FS-TABNAME.
ENDLOOP.
Table description is given in table DD02T.

Get value from a table and save it inside of a structure

I am new in ABAP and I have to modify these lines of code:
LOOP AT t_abc ASSIGNING <fs_abc> WHERE lgart = xyz.
g_abc-lkj = g_abc-lkj + <fs_abc>-abc.
ENDLOOP.
A coworker told me that I have to use a structure and not a field symbol.
How will be the syntax and why to use a structure in this case?
I have no idea why the co-worker wants that you use a structure in this case, because using a field symbol while looping is usually more performant. The reason could be that you are doing some kind of a novice training and he wants you to learn different syntax variants.
Using a structure while looping would like this
LOOP AT t_abc INTO DATA(ls_abc)
WHERE lgart = xyz.
g_abc-lkj = g_abc-lkj + ls_abc-abc.
ENDLOOP.
Your code is correct, because Field symbol functions almost the same as a structure.
For Field symbol
Field symbol is a pointer,
so there is no data copy action for field symbol, and the performance is better
Well if we changed the value via field symbol, the internal table get changed also
For Structure
Structure is a copy of the data, so there is a data copy action, and the performance is bad if the data row is bigger than 200 bytes (based on the SAP ABAP programming guide for performance)
If changed the data in the structure, the original internal table remains the same because there are 2 copies of the data in memory

How do I read and print a generic internal table?

I'm pretty new to this. I'm currently studying abap for my job and I have a problem that I can't seem to work out. How would I go about creating a Function Module that takes any kind of internal table and write it to the screen? I'm looking for a very generic solution that can work with any kind of internal table as input.
This is the exact reason SAP has developed ALV (ABAP List Viewer).
One of the shortest way to display any (non-nested) table is the following.
DATA: go_alv TYPE REF TO cl_salv_table.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = go_alv
CHANGING
t_table = itab.
go_alv->display( ).
This would be the simplest way for a table whose line type is only flat data objects, taken from page 33 of This SAP Development Guide.
FIELD-SYMBOLS: <row> TYPE ANY,
<comp> TYPE ANY.
LOOP AT itab INTO <row>.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <row> TO <wa_comp>.
IF sy-subrc <> 0.
SKIP.
EXIT.
ENDIF.
WRITE <wa_comp>.
ENDDO.
ENDLOOP.
A more robust way would be to use introspection containing Run-Time Type Services, a set of classes that let you introspect the details of a data object. This would give you the details mentioned by vwegert. An even better option would be to put it in an ALV grid.
It is possible, but you have to think about a lot of stuff:
column widths, dynamically sizing columns
currency fields / fields with units
date formatting in a multi-lingual environment
tables may contain arbitrary data, including nested tables
All things considered, this is not a trivial task. This is best left to the existing components.