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

Reuse cl_alv_table_create=>create_dynamic_table ?

Former Member
0 Likes
2,044

Hi Experts-

I need to create multiple internal tables in my ABAP report dynamically. At each run this ABAP report may need to create anywhere from 1 to n internal tables dynamically (based on inputted data).

I am on R/3 4.6C and used the code samples from the following weblogs:

  • /people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

  • /people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

The 'cl_alv_table_create=>create_dynamic_table' approach works okay when I try to create just one internal dynamically but short dumps if I try to reuse this code to create other internal tables dynanically.

After the use of the first dynamic internal table is done I unassign the field symbols for the dynamic table, work area and field contents. Then I reinvoke this method to create the next dynamic internal table and at that point the code short dumps.

As I mentioned, at this time the three field symbols are already unassigned (basically how they would have been at the first time this method was invoked for creating the first dynamic internal table).

Is it possible to create multiple dynamic internal tables by re-using this method and if so then how? Is this problem being caused more because of something to do with the data reference (pointer) that I derefence into the field symbol rather than the field symbols themselves that I am unassigning?

Thanks for your help.

- Vik.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,821

Hello Vik

The class CL_ALV_TABLE_CREATE uses a subroutine pool to generate the dynamic itabs. Calling this method more than 36 times will result in a short dump.

The code shown below shows how to dynamically create an unlimited number of itabs. Unfortunately, this variant of the CREATE DATA statement for table types is not yet available in 4.6c.

REPORT  zus_sdn_dynamic_create        .

DATA:
  gd_tabname   TYPE tabname,
  gdo_data     TYPE REF TO data.


FIELD-SYMBOLS:
  <gt_itab>    TYPE table.


START-OF-SELECTION.

  gd_tabname = 'KNA1'.
  CREATE DATA gdo_data TYPE STANDARD TABLE OF (gd_tabname).
  ASSIGN gdo_data->* to <gt_itab>.

END-OF-SELECTION.

Regards

Uwe

10 REPLIES 10
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,822

Hello Vik

The class CL_ALV_TABLE_CREATE uses a subroutine pool to generate the dynamic itabs. Calling this method more than 36 times will result in a short dump.

The code shown below shows how to dynamically create an unlimited number of itabs. Unfortunately, this variant of the CREATE DATA statement for table types is not yet available in 4.6c.

REPORT  zus_sdn_dynamic_create        .

DATA:
  gd_tabname   TYPE tabname,
  gdo_data     TYPE REF TO data.


FIELD-SYMBOLS:
  <gt_itab>    TYPE table.


START-OF-SELECTION.

  gd_tabname = 'KNA1'.
  CREATE DATA gdo_data TYPE STANDARD TABLE OF (gd_tabname).
  ASSIGN gdo_data->* to <gt_itab>.

END-OF-SELECTION.

Regards

Uwe

Read only

0 Likes
1,821

Hello Uwe,

Thank you for your response.

So, are you saying that in R/3 4.6C it is possible to re-use this method (eg., in a loop) to create up to 36 dynamic internal tables.

I do not think that this report will need to create 36 dynamic internal tables at any one run. At this point this method is short-dumping on trying to create just two dynamic internal tables in my code.

As I said, after the job of the first dynamic internal table is complete, I unassign its <FS> and also unassign the <FS> for the work area and the <FS> for the data element.

The short-dump happens when this method is called a second time.

May I ask you for code in 4.6C that will demonstrate the re-use of this method under 36 times?

Regards,

- Vik.

Read only

0 Likes
1,821

here's a sample which creates two internal tables successfully usung the same reference variables:

REPORT znrw_dyn_tab_MULTI.

TABLES dd03l.

parameters: p_tab1 type tabname, p_tab2 type tabname.

DATA dref TYPE REF TO data.

DATA tabdref TYPE REF TO data.

FIELD-SYMBOLS <struc> TYPE ANY.

FIELD-SYMBOLS <tab> TYPE table.

PERFORM get_data USING tabdref dref p_tab1.

read table <tab> index 1 assigning <struc>.

perform print using <struc>.

PERFORM get_data USING tabdref dref p_tab2.

read table <tab> index 1 assigning <struc>.

perform print using <struc>.

&----


*& Form get_data

&----


  • text

----


  • -->US_DREF text

----


FORM get_data

USING us_tdref type ref to data

us_dref TYPE REF TO data

us_table type tabname.

CREATE DATA us_tdref TYPE TABLE OF (us_table).

ASSIGN us_tdref->* TO <tab>.

SELECT * FROM (us_table) UP TO 20 ROWS INTO CORRESPONDING FIELDS OF

TABLE <tab>.

CREATE DATA us_dref like line of <tab>.

ASSIGN us_dref->* TO <struc>.

ENDFORM. "get_data

FORM print USING p_struc.

FIELD-SYMBOLS <field> TYPE ANY.

new-line.

DO.

*... work through all structure's components...

ASSIGN COMPONENT sy-index OF STRUCTURE p_struc TO <field>.

IF sy-subrc <> 0.

*... exit when reaching the end of components

EXIT.

ENDIF.

write: <field>.

ENDDO.

ENDFORM. " compare

Read only

0 Likes
1,821

Hi Neil,

Could you kindly elaborate on how your code functions (perhaps with a few comments maybe)?

I am trying to ascertain as to what your code does to refresh/clear the dynamic internal table, work area and field given that you cannot clear or refresh field symbols.

I do not see you UNASSIGN any variable(s) and so I am not sure if your code will use the structure of the first table's definition even though it won't fail (short-dump) when you call upon it with a new data dictionary structure.

Can you please explain as to how it is that when called first the 'get_data' form will cast the structure of the first table into <tab> and then empty/remove it out and fill it in with that of the second table into <tab>?

Thanks,

- Vik.

Read only

0 Likes
1,821

Hi Vik

copy and run the program in your own environment and you'll see it in action. A picture is worth a thousand words. Basically it takes in two table name parameters. For each parameter in turn it creates a dynamic table and reads in up to 20 rows from the named table and then reads the first entry from the table and prints out its component values. I ran it for MARA and EKPO but it should work for any two tables. It uses exactly the same reference variables in both cases but if you step through you will see that once the 2nd create happens the old data is replaced by the new. No unassign or free is needed. A create data basically can be repeated and makes obsolete the previous version. You could retain the data if you used distinct field symbols but in my example everything is reused so only the latest instance can be referred to.

If you are still having problems post the key parts of your program and someone may be able to spot the error.

Hope that helps. Neil.

Read only

0 Likes
1,821

Hi Neil,

The code you have provided does not compile on an SAP R/3 4.6C system.

The line of that causes a syntax check failiure is 'CREATE DATA us_tdref TYPE TABLE OF (us_table).'. The syntax error raised is, 'The type specification of TABLE is incomplete.'

Did you test your code on a SAP R/3 4.6C system? If so, does this compile properly on that system?

Thanks,

- Vik.

Read only

0 Likes
1,821

Hello Vik

The following coding creates 36 times an itab, then I get the short dump.

REPORT  ZUS_CREATE_DYNAMIC_TABLE      .


data:
  w_tab        type tabname  value 'KNA1',
  dref_itab    type ref to data.



data:
  t_fcat       type lvc_t_fcat.


field-symbols:
  <itab>    type standard table.



start-of-selection.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*       I_BUFFER_ACTIVE              =
      I_STRUCTURE_NAME             = w_tab
*       I_CLIENT_NEVER_DISPLAY       = 'X'
*       I_BYPASSING_BUFFER           =
    CHANGING
      CT_FIELDCAT                  = t_fcat
*     EXCEPTIONS
*       INCONSISTENT_INTERFACE       = 1
*       PROGRAM_ERROR                = 2
*       OTHERS                       = 3
            .
  IF SY-SUBRC <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

do 40 times.



  clear: dref_itab.
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
*        I_STYLE_TABLE             =
      IT_FIELDCATALOG           = t_fcat
    IMPORTING
      EP_TABLE                  = dref_itab
*        E_STYLE_FNAME             =
    EXCEPTIONS
      GENERATE_SUBPOOL_DIR_FULL = 1
      others                    = 2.
  IF SY-SUBRC <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



  unassign <itab>.
  assign dref_itab->* to <itab>.
  SELECT * FROM  (w_tab) into table <itab>.

  write: / syst-index.
enddo.

I assume that the premature short dump is due to an empty fieldcatalog. Make sure that you check

CHECK ( t_fcat IS NOT INITIAL ).

before you call the static method.

Regards

Uwe

Read only

0 Likes
1,821

Hi Vik,

sorry for the slow response... I am on 4.7 and it does compile cleanly. I guess you have to use the fm instead if this technique is unavailable to you.

Read only

Former Member
0 Likes
1,821

Hi vik,

1.

For this purpose,

in my program,

there is an INDEPENDENT FORM

whose inputs are

TABLE NAME / STRUCTURE NAME

and from those, it consructs dynamic table.

2. Here is the program.

the dynamic table name will be

<DYNTABLE>.

3. U can use this program (FORM in this program)

to generate any kind of internal table

by specifying TABLE NAME .

4.

REPORT abc.

*----


COMPULSORY

FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.

FIELD-SYMBOLS: <dynline> TYPE ANY.

DATA: lt TYPE lvc_t_fcat.

DATA: ls TYPE lvc_s_fcat.

FIELD-SYMBOLS: <fld> TYPE ANY.

DATA : fldname(50) TYPE c.

*----


parameters : iname LIKE dd02l-tabname.

*----


START-OF-SELECTION.

*----


PERFORM

PERFORM mydyntable USING lt.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable USING ptabname.

*----


Create Dyn Table From FC

FIELD-SYMBOLS: <fs_data> TYPE REF TO data.

FIELD-SYMBOLS: <fs_1>.

FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.

DATA: lt_data TYPE REF TO data.

data : lt TYPE lvc_t_fcat .

DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.

*----


CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'

EXPORTING

tabname = iname

TABLES

ddfields = ddfields.

.

*----


CONSTRUCT FIELD LIST

LOOP AT ddfields.

ls-fieldname = ddfields-fieldname.

APPEND ls TO lt.

ENDLOOP.

ASSIGN lt_data TO <fs_data>.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt

IMPORTING

ep_table = <fs_data>

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

*----


Assign Dyn Table To Field Sumbol

ASSIGN <fs_data>->* TO <fs_1>.

ASSIGN <fs_1> TO <fs_2>.

ASSIGN <fs_1> TO <dyntable>.

ENDFORM. "MYDYNTABLE

regards,

amit m.

Read only

Former Member
0 Likes
1,821

Hi Uwe,

This is my query regarding the creation of dynamic internal tables.

See I have created dynamic internal table through a loop, Each time I will get a different structure, so my requirement is like I need to retain all the created internal tables in a loop, and I will be using the same after that loop.

Please help is there any way to do this.