Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Dynamic data declaration

Former Member
0 Likes
717

I posted same thread into "Dictionary section"

Hi,

I have a following issue - I am not sure whether this is possible - so I need your feedback/expertise.

I have a custom table (example ZABC) with several fields in it - one of them being TABNAME . Content of that table ZABC is

MANDT TABNAME FIELDNAME ZCHECK

800 KNA1 KUNNR

800 KNA1 NAME1 X

800 KNA1 NAME2 X

800 KNA1 KUNNR

800 KNVV BZIRK X

800 KNAS KUNNR

800 KNAS STCEG

etc.

There is no rule how many entries will be in this ZABC table - I guess it will be less than 20 - but this is not business rule. Also do not know how many different "tables" could be specified.

Fields MANDT and ZCHECK fields are not important at this stage.

The challenge I have is to fetch data for only those fields from their specified physical tables (KNA1, KNVV, KNAS) in internal tables.

In this case I would need to have 3 internal tables one for KNA1, one for KNAS and one for KNVV.

I need to keep those "internal" table with content throughout the entire program for certain validation.

I know that I should dynamically define those internal tables but I have issues not knowing how many tables will be defined in ZABC table and I personally do not want to declare "<fs_itab> TYPE ANY TABLE" 20 or so times as fs_itab1 - fs_itab20.

Is there any way I can dynamically create as many internal tables as needed and keep their content throughout program run-time.

Regards

Goran

1 ACCEPTED SOLUTION
Read only

raja_thangamani
Active Contributor
0 Likes
625

Here is the sample code to create the Itab dynamically..


***** Creating Internal Table at Run time*******

DATA: LINETYPE  TYPE REF TO CL_ABAP_STRUCTDESCR,
      TABLETYPE TYPE REF TO CL_ABAP_TABLEDESCR,
      KEY       TYPE ABAP_KEYDESCR_TAB.

FIELD-SYMBOLS: <TABLE> TYPE ANY TABLE.

DATA: DREF TYPE REF TO DATA.

* Get Structure of Some Database Table for example

LINETYPE ?=   CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( 'SPFLI' ).

* Specify Key fields if needed

APPEND 'CARRID' TO KEY.
APPEND 'CONNID' TO KEY.

* Generate Internal table Type Object

TABLETYPE = CL_ABAP_TABLEDESCR=>CREATE(
    P_LINE_TYPE  = LINETYPE
    P_TABLE_KIND = CL_ABAP_TABLEDESCR=>TABLEKIND_SORTED
    P_UNIQUE     = ABAP_TRUE
    P_KEY        = KEY ).

* Create a data object of a specific type using a type object

CREATE DATA DREF TYPE HANDLE TABLETYPE.

ASSIGN DREF->* TO <TABLE>.

SELECT  * FROM SPFLI INTO TABLE <TABLE> WHERE CARRID = 'LH'.

Raja T

4 REPLIES 4
Read only

raja_thangamani
Active Contributor
0 Likes
626

Here is the sample code to create the Itab dynamically..


***** Creating Internal Table at Run time*******

DATA: LINETYPE  TYPE REF TO CL_ABAP_STRUCTDESCR,
      TABLETYPE TYPE REF TO CL_ABAP_TABLEDESCR,
      KEY       TYPE ABAP_KEYDESCR_TAB.

FIELD-SYMBOLS: <TABLE> TYPE ANY TABLE.

DATA: DREF TYPE REF TO DATA.

* Get Structure of Some Database Table for example

LINETYPE ?=   CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( 'SPFLI' ).

* Specify Key fields if needed

APPEND 'CARRID' TO KEY.
APPEND 'CONNID' TO KEY.

* Generate Internal table Type Object

TABLETYPE = CL_ABAP_TABLEDESCR=>CREATE(
    P_LINE_TYPE  = LINETYPE
    P_TABLE_KIND = CL_ABAP_TABLEDESCR=>TABLEKIND_SORTED
    P_UNIQUE     = ABAP_TRUE
    P_KEY        = KEY ).

* Create a data object of a specific type using a type object

CREATE DATA DREF TYPE HANDLE TABLETYPE.

ASSIGN DREF->* TO <TABLE>.

SELECT  * FROM SPFLI INTO TABLE <TABLE> WHERE CARRID = 'LH'.

Raja T

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
625

Hi,

The best thing would be to create an internal table of type Internal table.

That is

DATA: itab type any table.

DATA: it_itab like table of itab.

Regards,

Sesh

Read only

Former Member
0 Likes
625

All those examples you'all posted are great BUT it does not answer my question nor help me to solve my issue. Maybe I was not clear enough ... or something is wrong with me

Once again.

I have no problem to get a dynamically defined table and fill its content from ONE physicall table. That is pretty straight forward. The issue I have is that I do not know how many dynamically tables I will have to define and later asign to field symbol. Look at my custom table above and you will understand what I am trying to say.

What makes this issue more complex is that, once data fetch from those physicall SAP tables, I have to keep the content for all those internal tables during the entire program run . Second the most, I do not want to limit the program by defining FS "n" times in data declaration section. That is really important since I have no idea how many tables will be accessed.

If I follow the logic from your examples, I will end up overwritting the FS content every time I assign new internal table to FS.

In order to solve this issue, I was thinking that whenever I get the data from SAP table into internal table (field symbol) i export the content of internal table / FS to memory and then retrieve them when needed. This sounds like waky /fuzzy process and I am not aware of anything better. I am old fashion ABAP guy who got certified 1995 and have not seen ABAP for at least 5 years.

I would like that someone either tells me whether I am OK going forard with my "memory" solution or ... in few words gives me hint how to handle multiple dynamicall defined tables AT THE SAME TIME with its content using some OO code.

Thanks

G

P.S.

I have erroneusly opened the same thread twice. Sorry

Read only

0 Likes
625

this may be a workable solution.

just define on <fs> type any.

and each time you want an itab, use this as convert the content to xml and store it in a itab , for each new table append the xml to this itab. and later in your program whereever you want you create another dynamic itab based on the xml and conver tthe xml back to itab for processing. (did i confuse you?)

Regards

Raja