You are on page 1of 9

WITH TABLE KEY

Demo program

Program fetches 5 records fro Customer Master KNA1 table

DATA:
lt_kna1 TYPE TABLE OF kna1,
wa_kna1 type kna1.

SELECT * FROM kna1 INTO


TABLE lt_kna1 UP TO 5 ROWS.

IF sy-subrc eq 0 .
READ TABLE lt_kna1 into wa_kna1 WITH TABLE KEY kunnr = '0000000002'.
IF sy-subrc eq 0.

ENDIF.
ENDIF.

Output

Reason being we have not included primary key MANDT of table KNA1 in read operation

In table KNA1 we can see there are 2 primary key fields


Add MANDT
READ TABLE lt_kna1 into wa_kna1 WITH TABLE KEY mandt = sy-
mandt kunnr = '0000000002' .

Output

Error says, we need to add LAND1 to the read criteria.

From the above 2 examples we can confirm that if we are using with table key addition to read
statement then we need to maintain all the fields which are selected in table.

Version 2

REPORT ZTEST_SK.

TYPES:
BEGIN OF lty_kna1,
kunnr type kunnr,
land1 type land1,
END OF lty_kna1.

DATA:
lt_kna1 TYPE TABLE OF kna1,
wa_kna1 type kna1.

SELECT * FROM kna1 INTO


TABLE lt_kna1 UP TO 5 ROWS.

IF sy-subrc eq 0 .
READ TABLE lt_kna1 into wa_kna1 WITH TABLE KEY kunnr = '0000000002' .
IF sy-subrc eq 0.

ENDIF.
ENDIF.

Output

Version 3

REPORT ZTEST_SK.

TYPES:
BEGIN OF lty_kna1,
kunnr type kunnr,
land1 type land1,
END OF lty_kna1.

DATA:
lt_kna1 TYPE TABLE OF lty_kna1 with NON-UNIQUE KEY kunnr,
wa_kna1 type kna1.

SELECT kunnr land1 FROM kna1 INTO


TABLE lt_kna1 UP TO 5 ROWS.

IF sy-subrc eq 0 .
READ TABLE lt_kna1 into wa_kna1 WITH TABLE KEY kunnr = '0000000002' .
IF sy-subrc eq 0.
ENDIF.
ENDIF.

Output

Version 4

REPORT ZTEST_SK.

TYPES:
BEGIN OF lty_kna1,
kunnr type kunnr,
land1 type land1,
END OF lty_kna1.

DATA:
lt_kna1 TYPE TABLE OF lty_kna1,
wa_kna1 type kna1.

SELECT kunnr land1 FROM kna1 INTO


TABLE lt_kna1 UP TO 5 ROWS.

IF sy-subrc eq 0 .
READ TABLE lt_kna1 into wa_kna1 WITH TABLE KEY kunnr = '0000000002' .
IF sy-subrc eq 0.
WRITE:wa_kna1-kunnr.
ENDIF.
ENDIF.

Version 5

REPORT ZTEST_SK.

TYPES:
BEGIN OF lty_kna1,
kunnr type kunnr,
land1 type land1,
END OF lty_kna1.

DATA:
lt_kna1 TYPE TABLE OF lty_kna1,
wa_kna1 type kna1.

SELECT kunnr land1 FROM kna1 INTO


TABLE lt_kna1 UP TO 5 ROWS.

IF sy-subrc eq 0 .
READ TABLE lt_kna1 into wa_kna1 WITH TABLE KEY kunnr = '0000010009' lan
d1 = '' .
IF sy-subrc eq 0.
WRITE:wa_kna1-kunnr.
ENDIF.
ENDIF.

Debugger
Output

Addition of BINARY KEY


Addition of BINARY SEARCH is not permitted when WITH TABLE KEY is used in READ statement

WITH TABLE KEY uses linear search

STANDARD TABLE TYPE supports non-unique key while defining variables.


Version 6

REPORT ZTEST_SK.

TYPES:
BEGIN OF lty_kna1,
kunnr type kunnr,
land1 type land1,
END OF lty_kna1.

DATA:
lt_kna1 TYPE TABLE OF lty_kna1 WITH NON-UNIQUE KEY kunnr,
wa_kna1 type kna1.

SELECT kunnr land1 FROM kna1 INTO


TABLE lt_kna1 UP TO 5 ROWS.

IF sy-subrc eq 0 .
READ TABLE lt_kna1 into wa_kna1 WITH TABLE KEY kunnr = '0000010009' .
IF sy-subrc eq 0.
WRITE:wa_kna1-kunnr.
ENDIF.
ENDIF.

Output