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

Reading internal tables

Former Member
0 Likes
957

Hi all

i am using internal table itab1,

i want to add the similar contents of itab1 from itab2 into itab3

how to do it?

thanks

raghvendra bhanap

Hi all

i am using internal table itab1,

i want to add the similar contents of itab1 from itab2 into itab3

how to do it?

thanks

raghvendra bhanap

6 REPLIES 6
Read only

Former Member
0 Likes
930

itab2[] = itab1[].

itab3[] = itab1[].

Edited by: raj desai on May 1, 2008 10:46 AM

Read only

Former Member
0 Likes
930

Try...

loop at itab2 into wa_itab2.

read table itab1 into wa_itab1 with key......

if sy-subrc = 0.

append wa_tab1 to itab3.

endif.

endloop.

Read only

Former Member
0 Likes
930

Hi,

Try Following code.

HTAB1 = ITAB1.

REFRESH ITAB3.

LOOP AT ITAB2 ASSIGNING <WA>.

READ TABLE HTAB1 FROM <WA>

TRANSPORTING NO FIELDS.

IF SY-SUBRC = 0.

APPEND <WA> TO ITAB3.

ENDIF.

ENDLOOP.

FREE HTAB1.

Regards,

Nikita

Read only

Former Member
0 Likes
930

Read the help on APPEND LINES OF table1 to table3.

MattG.

Read only

Former Member
0 Likes
930

Hi,

This is a documentation regarding reading internal tables.

Hope this will be useful...

When the size of the record or the number of records in the internal table is large, doing a linear search is time consuming. It is always a good practice to search a record by binary search (Always Sort the table before doing a binary search). The difference is felt especially in the production environment where the live data is usually huge. As of release 4.0 there are new types of internal tables like SORTED and HASHED which can be effectively used to further reduce the search time and processing time.

e.g. Do not use the following statement:-

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Loop at I_mara.

-


Read table i_marc with key matnr = I_mara-matnr.

-


Endloop.

Instead use the following statement:-

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Sort I_marc by matnr.

Loop at I_mara.

-


Read table i_marc with key

matnr = I_mara-matnr

binary search.

-


Endloop.

It is a good practice to search records from internal tables using a binary search. But extreme caution needs to be applied as it may either increase the time or may cause run time termination if it is not sorted.

Always the sort the internal table by the required keys before performing a binary search.

e.g. Do not use the following statement:-

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Loop at I_mara.

-


Read table i_marc with key matnr = I_mara-matnr binary search.

-


Endloop.

Instead use the following statement:-

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Sort I_marc by matnr.

Loop at I_mara.

-


Read table i_marc with key

matnr = I_mara-matnr

binary search.

-


Endloop.

It is a general practice to use Read table <itab>… This statement populates all the values of the structure in the workarea.

The effect is many fold:-

• It increases the time to retrieve data from internal table

• There is large amount of unused data in work area

• It increases the processing time from work area later

It is always a good practice to retrieve only the required fields. Always use the syntax Read table <itab> transporting f1 f2 … FN … If just a check is being performed for existence of a record use Read table <itab> transporting no fields …

e.g. Do not use the following statement:-

data: i_vbak like vbak occurs 0 with header line.

data: i_vbap like vbap occurs 0 with header line.

Loop at i_vbak.

-


read table i_vbap with key

vbeln = i_vbak-vbeln binary search.

If sy-subrc = 0 and i_vbap-posnr = ‘00010’.

-


-


endif.

-


Endloop.

Instead use the following statement:-

data: i_vbak like vbak occurs 0 with header line.

data: i_vbap like vbap occurs 0 with header line.

Loop at i_vbak.

-


read table i_vbap transporting posnr with key

vbeln = i_vbak-vbeln binary search.

If sy-subrc = 0 and i_vbap-posnr = ‘00010’.

-


-


endif.

-


Endloop.

There are many ways in which a select statement can be optimized. Effective use of primary and secondary indexes is one of them. Very little attention is paid especially to the secondary indexes. The following points should be noted before writing a select statement:-

• Always use the fields in the where clause in the same order as the keys in the database table

• Define the secondary indexes in the database for the fields which are most frequently used in the programs

• Always try to use the best possible secondary index in the where clause if it exists

• Do not have many secondary indexes defined for a table

• Use as many keys both primary and secondary as possible to optimize data retrieval

As of release 4.5 it is now possible to define the secondary index in the where clause using %_HINT.

e.g. Do not use the following statement:-

Assuming a secondary index is defined on the field vkorg in table vbak

Select vbeln vkorg from vbak

Into table i_vbak

Where vbeln in s_vbeln.

Loop at i_vbak.

Case i_vbak-vkorg.

When ‘IJI1’.

-


-


When ‘IJI2’.

-


-


Endcase.

Endloop.

Instead use the following statement:-

Select vbeln vkorg from vbak

Into table i_vbak

Where vbeln in s_vbeln and

Vkorg in (‘IJI1’,’IJI2’).

Loop at i_vbak.

Case i_vbak-vkorg.

When ‘IJI1’.

-


-


When ‘IJI2’.

-


-


Endcase.

Endloop.

Read only

Former Member
0 Likes
930

Hi,

This is a documentation regarding reading internal tables.

Hope this will be useful...

When the size of the record or the number of records in the internal table is large, doing a linear search is time consuming. It is always a good practice to search a record by binary search (Always Sort the table before doing a binary search). The difference is felt especially in the production environment where the live data is usually huge. As of release 4.0 there are new types of internal tables like SORTED and HASHED which can be effectively used to further reduce the search time and processing time.

e.g. Do not use the following statement:-

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Loop at I_mara.

-


Read table i_marc with key matnr = I_mara-matnr.

-


Endloop.

Instead use the following statement:-

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Sort I_marc by matnr.

Loop at I_mara.

-


Read table i_marc with key

matnr = I_mara-matnr

binary search.

-


Endloop.

It is a good practice to search records from internal tables using a binary search. But extreme caution needs to be applied as it may either increase the time or may cause run time termination if it is not sorted.

Always the sort the internal table by the required keys before performing a binary search.

e.g. Do not use the following statement:-

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Loop at I_mara.

-


Read table i_marc with key matnr = I_mara-matnr binary search.

-


Endloop.

Instead use the following statement:-

Select matnr from mara

Into table i_mara

Where matnr in s_matnr.

Select matnr werks from marc

Into table i_marc

For all entries in i_mara

Where matnr eq i_mara-matnr.

Sort I_marc by matnr.

Loop at I_mara.

-


Read table i_marc with key

matnr = I_mara-matnr

binary search.

-


Endloop.

It is a general practice to use Read table <itab>… This statement populates all the values of the structure in the workarea.

The effect is many fold:-

• It increases the time to retrieve data from internal table

• There is large amount of unused data in work area

• It increases the processing time from work area later

It is always a good practice to retrieve only the required fields. Always use the syntax Read table <itab> transporting f1 f2 … FN … If just a check is being performed for existence of a record use Read table <itab> transporting no fields …

e.g. Do not use the following statement:-

data: i_vbak like vbak occurs 0 with header line.

data: i_vbap like vbap occurs 0 with header line.

Loop at i_vbak.

-


read table i_vbap with key

vbeln = i_vbak-vbeln binary search.

If sy-subrc = 0 and i_vbap-posnr = ‘00010’.

-


-


endif.

-


Endloop.

Instead use the following statement:-

data: i_vbak like vbak occurs 0 with header line.

data: i_vbap like vbap occurs 0 with header line.

Loop at i_vbak.

-


read table i_vbap transporting posnr with key

vbeln = i_vbak-vbeln binary search.

If sy-subrc = 0 and i_vbap-posnr = ‘00010’.

-


-


endif.

-


Endloop.

There are many ways in which a select statement can be optimized. Effective use of primary and secondary indexes is one of them. Very little attention is paid especially to the secondary indexes. The following points should be noted before writing a select statement:-

• Always use the fields in the where clause in the same order as the keys in the database table

• Define the secondary indexes in the database for the fields which are most frequently used in the programs

• Always try to use the best possible secondary index in the where clause if it exists

• Do not have many secondary indexes defined for a table

• Use as many keys both primary and secondary as possible to optimize data retrieval

As of release 4.5 it is now possible to define the secondary index in the where clause using %_HINT.

e.g. Do not use the following statement:-

Assuming a secondary index is defined on the field vkorg in table vbak

Select vbeln vkorg from vbak

Into table i_vbak

Where vbeln in s_vbeln.

Loop at i_vbak.

Case i_vbak-vkorg.

When ‘IJI1’.

-


-


When ‘IJI2’.

-


-


Endcase.

Endloop.

Instead use the following statement:-

Select vbeln vkorg from vbak

Into table i_vbak

Where vbeln in s_vbeln and

Vkorg in (‘IJI1’,’IJI2’).

Loop at i_vbak.

Case i_vbak-vkorg.

When ‘IJI1’.

-


-


When ‘IJI2’.

-


-


Endcase.

Endloop.