ABAP sorted table insert TABLE_ILLEGAL_STATEMENT SAP error

Remember when doing a sorted table insert you should use the INSERT ABAP statement instead of APPEND, but maybe you have done this but you are getting a TABLE_ILLEGAL_STATEMENT short dump. Even though when you perform an ABAP syntax check you don’t get any error!

Does your code look something like this

INSERT wa_tab into it_tab. 

???

Full code listing to demonstrate insert error


*&---------------------------------------------------------------------*
*& Report  ZINSERT_ERROR
*&
*&---------------------------------------------------------------------*
*& demonstrate and provide fix to table illegal insert error
*&
*&---------------------------------------------------------------------*
REPORT  ZINSERT_ERROR.

data: it_ekko type STANDARD TABLE OF ekko,
      wa_ekko type ekko.


*This code will cause the table illegal insert error
SELECT *
  up to 10 rows
  from ekko
  into corresponding fields of wa_ekko.


 INSERT wa_ekko into it_ekko.
* Replace INSERT with follwoing to fix issue
* INSERT wa_ekko into TABLE it_ekko.

ENDSELECT.

You have probably also tried

APPEND wa_tab to it_tab

and it works fine!!

So what is wrong? well, it’s something very simple…. you should basically use “INTO TABLE” instead of just “INTO” when adding new entries to a SORTED table.

INSERT wa_tab into TABLE it_tab.

Give this a try and it should now work.

Analysis for sorted table insert Error

Here are the full details of the error I was getting before I added the into TABLE
table illegal statement

You attempted to change, delete or create a line in the
internal table “\PROGRAM=ZMYPROGRAM\DATA=IT_DATA”, but no valid
cursor exists
for the table.
Possible reasons:
1. The relevent ABAP/4 statement does not include the addition
“…INDEX…”, although the statement is not
inside a “LOOP…ENDLOOP” loop processing this table.
2. The relevent ABAP/4 statement was called from within a
“LOOP…ENDLOOP” loop after a DELETE
“\PROGRAM=ZMYPROGRAM\DATA=IT_DATA”.