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

Populating Internal Table whose name is determined dynamically

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,146

Hello Guys,

I have a peculiar requirement for splitting the data retrieved in sets of 10000 records. For e.g., if there are say 21000 records from the Database. I populate 3 internal tables: 2 tables with 10000 entries each & 1 table with 1000 records.

Can you guys suggest something?

PS: All the internal tables have the same structure

Rgds,

Suhas

1 ACCEPTED SOLUTION
Read only

raviprakash
Product and Topic Expert
Product and Topic Expert
0 Likes
878

>

> Hello Guys,

>

> I have a peculiar requirement for splitting the data retrieved in sets of 10000 records. For e.g., if there are say 21000 records from the Database. I populate 3 internal tables: 2 tables with 10000 entries each & 1 table with 1000 records.

>

> Can you guys suggest something?

>

> PS: All the internal tables have the same structure

>

> Rgds,

> Suhas

Hi Suhas,

Here is a code that will exactly do what you have asked for:-

* Data Declarations.....................................................
TYPES: BEGIN OF tt_dref,
        dref        TYPE REF TO data,
       END OF tt_dref.

DATA: gt_dref       TYPE TABLE OF tt_dref,
      gs_dref       TYPE tt_dref,
      gt_tab        TYPE TABLE OF {DATABASE},
      gt_tab_temp   TYPE TABLE OF {DATABASE},
      gv_count      TYPE i,
      gs_tab        LIKE LINE OF gt_tab.

CONSTANTS: gc_limit TYPE i VALUE '10000'.

FIELD-SYMBOLS <gfs_tab> TYPE table.
* End of Data Declarations..............................................


SELECT * FROM hrp{DATABASE} INTO TABLE gt_tab.

*Loop at the bigger table and break it into smaller blocks
*specified by the GC_LIMIT constant.
LOOP AT gt_tab INTO gs_tab.

  APPEND gs_tab TO gt_tab_temp.
  ADD 1 TO gv_count.

  IF gv_count EQ gc_limit.

    CREATE DATA gs_dref-dref TYPE STANDARD TABLE OF ('{DATABASE}').
    APPEND gs_dref TO gt_dref.
    ASSIGN gs_dref-dref->* TO <gfs_tab>.
    <gfs_tab> = gt_tab_temp.

    CLEAR: gv_count, gt_tab_temp, gs_dref.

  ENDIF.

ENDLOOP.

*Check whether the Data Reference table has the values
*for each of the packages.
LOOP AT gt_dref INTO gs_dref.

  ASSIGN gs_dref-dref->* TO <gfs_tab>.

ENDLOOP.

NOTE: Replace by the name of your database which has 21000 entries.

Many Regards,

Ravi.

6 REPLIES 6
Read only

Former Member
0 Likes
878

let ITAB be the main table with 21000 recored.

Loop at ITAB.

if sy-tabix < 10000.

append itab to itab1.

elseif sy-tabix > 10000 and sy-tabix <= 20000.

append itab to itab2.

elseif sy-tabix > 20000.

append itab to itab3.

endif.

endloop.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
878

Hello Amit,

Please see that i have said 21000 is an example. The main internal table may contain any amount of data. I think we can decide the no of internal tables @ run time.

BR,

Suhas

Read only

Former Member
0 Likes
878

Hi,

Have a look at this code....


DATA: dref TYPE REF TO data,
      dref1 TYPE REF TO data,
      BEGIN OF tab_store OCCURS 0,         "<----table to store all ur final tables....
        no TYPE i,
        tab TYPE REF TO data,
      END OF tab_store,
      tabix type sy-tabix,
      no TYPE i.

FIELD-SYMBOLS: <tab> TYPE STANDARD TABLE,
               <fs> LIKE LINE OF tab_store.


  CREATE DATA dref1 LIKE LINE OF tab_store.
  ASSIGN dref1->* TO <fs>.

  LOOP AT it_final INTO wa_final.                "<----breaking up ur final table
    APPEND wa_final TO it_final1.
    tabix = tabix + 1.
    IF tabix = 10000.                      "Checking tabix for 10000 records
      no = no + 1.
      CREATE DATA dref TYPE STANDARD TABLE OF t_final.               "Creating small tables
      ASSIGN dref->* TO <tab>.
      APPEND LINES OF it_final1 TO <tab>.             "Copying ur small tables....
      <fs>-tab = dref.                  "Storing u small tables....
      <fs>-no = no.
      APPEND <fs> TO tab_store.        "<------All final tables in this table....
      CLEAR tabix.
    ENDAT.
    CLEAR:wa_final,
           tabix.
  ENDLOOP.

Read only

Former Member
0 Likes
878

Hi,

First get all the Data into a Single Internal Table itab...Then use Describe Statement and if the Table Rows are more that 10000, then declare one more table there only and Transfer the Records from 10001 to 20000, and so on for the records after 20000, if exists....

Select *

from

into table itab.

if sy-subrc = 0.

if sy-dbcnt > 10000 and sy-dbcnt <= 20000.

Data: itab1 like standard table of itab.

loop at itab from 10001 to 20000.

append itab to itab1.

endloop

endif.

if sy-dbcnt > 10000 and sy-dbcnt > 20000.

Data: itab2 like standard table of itab.

loop at itab from 20001 to sy-dbcnt.

append itab to itab3.

endloop

endif.

Read only

raviprakash
Product and Topic Expert
Product and Topic Expert
0 Likes
879

>

> Hello Guys,

>

> I have a peculiar requirement for splitting the data retrieved in sets of 10000 records. For e.g., if there are say 21000 records from the Database. I populate 3 internal tables: 2 tables with 10000 entries each & 1 table with 1000 records.

>

> Can you guys suggest something?

>

> PS: All the internal tables have the same structure

>

> Rgds,

> Suhas

Hi Suhas,

Here is a code that will exactly do what you have asked for:-

* Data Declarations.....................................................
TYPES: BEGIN OF tt_dref,
        dref        TYPE REF TO data,
       END OF tt_dref.

DATA: gt_dref       TYPE TABLE OF tt_dref,
      gs_dref       TYPE tt_dref,
      gt_tab        TYPE TABLE OF {DATABASE},
      gt_tab_temp   TYPE TABLE OF {DATABASE},
      gv_count      TYPE i,
      gs_tab        LIKE LINE OF gt_tab.

CONSTANTS: gc_limit TYPE i VALUE '10000'.

FIELD-SYMBOLS <gfs_tab> TYPE table.
* End of Data Declarations..............................................


SELECT * FROM hrp{DATABASE} INTO TABLE gt_tab.

*Loop at the bigger table and break it into smaller blocks
*specified by the GC_LIMIT constant.
LOOP AT gt_tab INTO gs_tab.

  APPEND gs_tab TO gt_tab_temp.
  ADD 1 TO gv_count.

  IF gv_count EQ gc_limit.

    CREATE DATA gs_dref-dref TYPE STANDARD TABLE OF ('{DATABASE}').
    APPEND gs_dref TO gt_dref.
    ASSIGN gs_dref-dref->* TO <gfs_tab>.
    <gfs_tab> = gt_tab_temp.

    CLEAR: gv_count, gt_tab_temp, gs_dref.

  ENDIF.

ENDLOOP.

*Check whether the Data Reference table has the values
*for each of the packages.
LOOP AT gt_dref INTO gs_dref.

  ASSIGN gs_dref-dref->* TO <gfs_tab>.

ENDLOOP.

NOTE: Replace by the name of your database which has 21000 entries.

Many Regards,

Ravi.

Read only

raviprakash
Product and Topic Expert
Product and Topic Expert
0 Likes
878

Hi Suhas Saha,

Here is the explanation to my reply to your post. Since we need to create the internal tables dynamically at the runtime, we would require an array(table) which can keep the pointers to the respective internal tables created dynamically. Thus in the first part of the report, i declare my own type to keep the pointers to the dynmic internal tables:-

TYPES: BEGIN OF tt_dref,
        dref        TYPE REF TO data,
       END OF tt_dref.

Since in SE38 editor environment we cannot create nested structures for internal tables unlike in SE11(for dictionary tables), we can achieve this using the "Data Reference" variables. So whenever we create a new table and fill it, we save the pointer to it in our "Data Reference" table, in this case "gt_dref ".

Later when we require to retrieve the information out of this "Data Reference" table, we loop at it and then get the values in a Field Symbol, by using the syntax ASSIGN gs_dref-dref->* TO <gfs_tab>.

Thus we had to create a Field Symbol of type Table for the same. Let me know incase you require any assistance in understanding the code.

Many Regards,

Ravi.