cancel
Showing results for 
Search instead for 
Did you mean: 

Read Table with Key ,Primary or Secondar?

04-29-2020 5:17 PM
5114 views 5 comments Go to solution
0 Likes
SAP Managed Tags
Subscribe

Hi all,

I have been reading through the ABAP 740 read table statements.

Questions:

I have a ztable with field1 to field10 with only field1 as primary key.

1) I am reading through a itab in my program with " read table into wa with key field2 = variable(from loop wa)"

this field2 is not specified as key , so i will be getting the first found row, right?. if yes my derived result may or may not be right.

2) or should i declare as "WITH NON-UNIQUE SORTED KEY field2_field3 COMPONENTS field2 field3.

3) Because i will have to read the itab with field2 or field3 several times in my code.

0 Likes

Accepted Solutions (1)

Accepted Solutions (1)

michael_piesche
Active Contributor
" 1) I am reading through a itab in my program with "read table into wa with key field2 = variable(from loop wa)"
" this field2 is not specified as key , so i will be getting the first found row, right?. if yes my derived result may or may not be right.

" READ statement to match just on record based on non-explicit key
READ TABLE table INTO wa WITH KEY field2 = variable.
IF sy-subrc = 0.
  " ...
ENDIF.

As field2 is not part of the primary key and no secondary keys exist, your only current option is to use "WITH KEY field2", which will result in a sequential search looping through all the records of the table until the first matching record is found. So your result will definitly be right, if there is no match or exatly one match. If there are more then one matches, it depends on your requirement whether that one match is the right one, or whether another one are all need to be evaluated instead. In the worst case, if there is no match, the search will have to be performed on all records in the table, resulting in a worst case performance of O(n), whereas n is the number of records.

" 2) or should i declare as "WITH NON-UNIQUE SORTED KEY field2_field3 COMPONENTS field2 field3.

" Example of how to define a table type with a primary key and a secondary key with two fields
DATA table TYPE SORTED TABLE OF tabletype WITH UNIQUE KEY field1
           WITH NON-UNIQUE SORTED KEY field2_field3 COMPONENTS field2 field3.

" READ statement to match just on record based on partial secondary key
READ TABLE table INTO wa WITH KEY field2_field3 COMPONENTS field2 = variable.
IF sy-subrc = 0.
  " ...
ENDIF.

This has the almost 'same' outcome as the above READ statement without a secondary key, but the performance will be better in case of large amounts of records (more than thousands and more to be relevant in terms of 'waiting time' for computation. And it will also stop the search when the first matching record is found. But it might not be the same record found by the above statement, if there are more than one matches, because of the search logic.

The difference is, that in this case, there is a binary search performed on the table records, dividing the search always in two equal parts, starting in the middle and depending on whether the current records field is smaller or bigger than the search term, it will continue the search in the upper or lower part. The worst case performance in this case is therefore only logarithmic with O(log n).

(Be aware, that in this case, also when just using a partial key, your key components in the READ statement are accessed in the order they are assigned from first to last of all compenents or from first to last of the partial components.)

" 3) Because i will have to read the itab with field2 or field3 several times in my code.

" Example of how to define a table type with a primary key and a secondary key with two fields
DATA table TYPE SORTED TABLE OF tabletype WITH UNIQUE KEY field1
           WITH NON-UNIQUE SORTED KEY field2_field3 COMPONENTS field2 field3.

" LOOP statement to match multiple records based on secondary key
LOOP AT table INTO wa USING KEY field2_field3 WHERE field2 = variable.
  " ...
ENDLOOP.

You have not described your requirement completly, but from what I get, probably a LOOP through the records might be better, in order to evaluate all possible matches for field2 based on variable.

(Be aware, that in this case, also when just using a partial key, your key components in the LOOP statement are accessed in the order they are assigned from first to last of all compenents or from first to last of the partial components.)

And if you have to read based on field2 or field3 seperately, and you want to use secondary keys, you will have to create to separate keys for field2 and field3, so that you can access them separately too:

" Example of how to define a table type with a primary key and two secondary keys
DATA: table TYPE SORTED TABLE OF tabletype WITH UNIQUE KEY field1
            WITH NON-UNIQUE SORTED KEY field2 COMPONENTS field2
            WITH NON-UNIQUE SORTED KEY field3 COMPONENTS field3.

" READ statement to match just on record based on secondary

READ TABLE table INTO wa WITH KEY field2 COMPONENTS field2 = variable1.
IF sy-subrc = 0.
  " ...
ENDIF.

READ TABLE table INTO wa WITH KEY field3 COMPONENTS field3 = variable2.
IF sy-subrc = 0.
  " ...
ENDIF.

" LOOP statement to match multiple records based on secondary key

LOOP AT table INTO wa USING KEY field2 WHERE field2 = variable1.
  " ...
ENDLOOP.

LOOP AT table INTO wa USING KEY field3 WHERE field3 = variable2.
  " ...
ENDLOOP.

Answers (0)