When selecting data into an internal table using the SELECT [...] INTO TABLE
syntax, do I need to ensure that I clear that table first?
Will the SELECT statement always clear the table for me, even if it fails?
Do I need to clear my internal table before a SELECT statement?
No, you don't. A SELECT [...] INTO TABLE
statement will always clear the target table , regardless of whether the statement completed successfully or not.
As the SAP Keyword Documentation states :
If
INTO
is used, the internal table is initialized. Previous rows remain intact ifAPPENDING
is used.
So a SELECT
with INTO TABLE
will always clear the table while one with APPENDING TABLE
will always preserve the original entries, regardless of whether the query succeeded or not.
And because I don't always trust documentation I ran a quick test myself:
DATA:
lt_table TYPE STANDARD TABLE OF but000.
SELECT *
UP TO 20 ROWS
FROM but000
INTO TABLE lt_table.
WRITE :/ |First SELECT. Table contains: { lines( lt_table ) } rows|.
SELECT *
UP TO 10 ROWS
FROM but000
INTO TABLE lt_table.
WRITE :/ |Second SELECT - { lines( lt_table ) } rows|.
SELECT *
UP TO 5 ROWS
FROM but000
APPENDING TABLE lt_table.
WRITE :/ |APPEND - { lines( lt_table ) } rows|.
SELECT *
UP TO 5 ROWS
FROM but000
APPENDING TABLE lt_table
WHERE partner = 0.
WRITE :/ |Failed APPEND - { lines( lt_table ) } rows|.
SELECT *
UP TO 5 ROWS
FROM but000
INTO TABLE lt_table
WHERE partner = 0.
WRITE :/ |Failed SELECT - { lines( lt_table ) } rows|.
This report will output:
First SELECT. Table contains: 20 rows
Second SELECT - 10 rows
APPEND - 15 rows
Failed APPEND - 15 rows
Failed SELECT - 0 rows
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.