Using an internal table in a ABAP query

Oct 24th, 2009 | By Ole | Category: Featured, SAP ABAP, SAP Tables & Views

In the following example we will populate an internal table and use it as an input to a database query.

  1. Define which material numbers we want to included in the query.
  2. Optimize the query.
  3. Run the query against the MARA table.
  4. Print the result of the query (number of rows retrieved)

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
*&---------------------------------------------------------------------*
*& Report  Z0OV_IN_INTERNAL_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  Z0OV_IN_INTERNAL_TABLE.
DATA:
  it_query TYPE STANDARD TABLE OF mara,
  wa_query TYPE mara,
  it_mara  TYPE STANDARD TABLE OF mara,
  l_lines  TYPE INT2.

* Populate the internal table it_query with the material numbers
* we will search for in the following query of the MARA table.
wa_query-matnr = '000000000000300007'.
APPEND wa_query TO it_query.
wa_query-matnr = '000000000000300008'.
APPEND wa_query TO it_query.

* Delete duplicates before running query (optimize the query)
DELETE ADJACENT DUPLICATES FROM it_query COMPARING matnr.

* Check if it_query is populated before running query.
IF NOT  it_query[] IS INITIAL.

  SELECT *
  FROM mara
  INTO CORRESPONDING FIELDS OF TABLE it_mara
  FOR ALL ENTRIES IN it_query
  WHERE matnr = it_query-matnr.

* Check and print how many rows we found.
  DESCRIBE TABLE it_mara  LINES l_lines.
    WRITE:/ 'Found ', l_lines, ' lines.'.

ELSE.
  WRITE:/ 'Your query is empty!'.
ENDIF.

Leave Comment

You must be logged in to post a comment.