简体   繁体   中英

read from internal table with loop and no results

In the below code, I read data from database table with SELECT into the internal table dresult .

Reading the first line of the internal table displays the expected result, but using the LOOP displays nothing.

What's the issue?

Note: I tried to remove ENDSELECT and it worked. Why?

REPORT ZTEST02.
    "Detail Informaion
TYPES : BEGIN OF details,
      d1 type arktx,        " description
      d2 type lfimg,        "quantity  
END OF details.

DATA : dresult   TYPE TABLE OF details WITH HEADER LINE,
       t_dresult TYPE details,
       sdno      TYPE vbeln.

PARAMETERS packo TYPE vbeln OBLIGATORY MATCHCODE OBJECT f4_likp.

START-OF-SELECTION.

SELECT arktx,lfimg
    INTO @dresult
    FROM lips as detail 
    LEFT JOIN marm as material
    ON detail~matnr = material~matnr
    LEFT OUTER JOIN vbak
    ON detail~vgbel = vbak~vbeln
WHERE detail~vbeln = @packo.

ENDSELECT.

READ TABLE dresult into t_dresult INDEX 1.
write: t_dresult-d1,t_dresult-d2.

LOOP AT dresult INTO t_dresult.
  write: t_dresult-d1,t_dresult-d2.
ENDLOOP.

With SELECT ... ENDSELECT you select the data into a work area ( INTO @dresult - which is actually the header line of the internal table with the same name). As result the internal table dresult does not contain any data, so the LOOP and the READ TABLE won't work.

I would declare the internal table without HEADER LINE and remove ENDSELECT :

DATA: dresult type STANDARD TABLE OF details,

...

SELECT ...
       INTO TABLE @dresult

Moreover, after READ TABLE , it is always good to check if an entry was found:

READ TABLE ...
IF sy-subrc EQ 0.
... 
ENDIF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM