Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Dynamic WHERE Statement for Deletion

Former Member
0 Likes
888

Hi ABAP Experts,

I like to delete several lines of an internal table using dynamic WHERE Statement.

My idea was the following, but it is not accepted by the compiler:

DELETE itab where (lt_where).

For select statement it works:

select count(*) from sflight where (lt_where)

Is there any other way to delete dynamically?

Thank you very much in advance.

Kind regards

Axel

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
835

Hi Axel,

use syntax:

Delete itab where field(f1) not in lt_itab.

Hope this can solve your problems.

Regards

Tutun

Hi

No, I don't think it's possible to use dynamic where condition, because it can be used for SELECT statament only,

but u can try to use the field-symbols:

DATA: BEGIN OF T_WHERE OCCURS 0,
        FIELD(30) TYPE C,
        VALUE(30) TYPE C,
      END    OF T_WHERE.
      
FIELD-SYMBOLS: <FS> TYPE ANY.
      
      
..................
      
LOOP AT ITAB.
  LOOP AT T_WEHERE.
    ASSIGN COMPONENT T_WHERE-FIELD OF STRUCTURE ITAB TO <FS>.
    IF <FS> = T_WHERE-VALUE.
      OK_DELETE = 'X'.
    ELSE.
      CLEAR OK_DELETE.
      EXIT.
    ENDIF.
  ENDLOOP.
  CHECK OK_DELETE = 'X'.
  DELETE ITAB.
ENDLOOP. 

Or u can try to use statament DELETE with option WITH TABLE KEY, here u can assign dynamically the field:

.DATA: BEGIN OF ITAB OCCURS 0,
       FIELD1,
       FIELD2,
     END OF ITAB.

DO 4 TIMES.
  ITAB-FIELD1 = 'A'.
  ITAB-FIELD2 = 'B'.
  APPEND ITAB.
ENDDO.

DO 4 TIMES.
  ITAB-FIELD1 = 'C'.
  ITAB-FIELD2 = 'B'.
  APPEND ITAB.
ENDDO.


LOOP AT ITAB.
  WRITE: / ITAB.
ENDLOOP.

DATA: FIELD1(30), FIELD2(30).

FIELD1 = 'FIELD1'.
FIELD2 = 'FIELD2'.
DO.
  DELETE TABLE ITAB WITH TABLE KEY (FIELD1) = 'A'
                                   (FIELD2) = 'B'.
  IF SY-SUBRC <> 0. EXIT. ENDIF.
ENDDO.
SKIP.

LOOP AT ITAB.
  WRITE: / ITAB.
ENDLOOP.

.

Max

3 REPLIES 3
Read only

Former Member
0 Likes
836

Hi Axel,

use syntax:

Delete itab where field(f1) not in lt_itab.

Hope this can solve your problems.

Regards

Tutun

Read only

0 Likes
835

Hi tutun,

thanks for you answer, thats it for what I'm was looking for.

Rewarding points.

Regards

Axel

Read only

Former Member
0 Likes
835

Hi

No, I don't think it's possible to use dynamic where condition, because it can be used for SELECT statament only,

but u can try to use the field-symbols:

DATA: BEGIN OF T_WHERE OCCURS 0,
        FIELD(30) TYPE C,
        VALUE(30) TYPE C,
      END    OF T_WHERE.
      
FIELD-SYMBOLS: <FS> TYPE ANY.
      
      
..................
      
LOOP AT ITAB.
  LOOP AT T_WEHERE.
    ASSIGN COMPONENT T_WHERE-FIELD OF STRUCTURE ITAB TO <FS>.
    IF <FS> = T_WHERE-VALUE.
      OK_DELETE = 'X'.
    ELSE.
      CLEAR OK_DELETE.
      EXIT.
    ENDIF.
  ENDLOOP.
  CHECK OK_DELETE = 'X'.
  DELETE ITAB.
ENDLOOP. 

Or u can try to use statament DELETE with option WITH TABLE KEY, here u can assign dynamically the field:

.DATA: BEGIN OF ITAB OCCURS 0,
       FIELD1,
       FIELD2,
     END OF ITAB.

DO 4 TIMES.
  ITAB-FIELD1 = 'A'.
  ITAB-FIELD2 = 'B'.
  APPEND ITAB.
ENDDO.

DO 4 TIMES.
  ITAB-FIELD1 = 'C'.
  ITAB-FIELD2 = 'B'.
  APPEND ITAB.
ENDDO.


LOOP AT ITAB.
  WRITE: / ITAB.
ENDLOOP.

DATA: FIELD1(30), FIELD2(30).

FIELD1 = 'FIELD1'.
FIELD2 = 'FIELD2'.
DO.
  DELETE TABLE ITAB WITH TABLE KEY (FIELD1) = 'A'
                                   (FIELD2) = 'B'.
  IF SY-SUBRC <> 0. EXIT. ENDIF.
ENDDO.
SKIP.

LOOP AT ITAB.
  WRITE: / ITAB.
ENDLOOP.

.

Max