András
András

Reputation: 1380

OpenSQL with comparison between columns

Can I compare columns of a table in OpenSQL?

Currently the code looks like this:

  SELECT SINGLE menge wemng ebeln ebelp
    INTO (eket-menge,
          eket-wemng,
          eket-ebeln,
          eket-ebelp)
    FROM eket
    WHERE ebeln = ekpo-ebeln
      AND ebelp = ekpo-ebelp.

  IF eket-menge NE eket-wemng.

I want to avoid retreiving lines where menge = wemng, but this does not work:

  SELECT menge wemng ebeln ebelp
    INTO (eket-menge,
          eket-wemng,
          eket-ebeln,
          eket-ebelp)
    FROM eket
    WHERE ebeln = ekpo-ebeln
      AND ebelp = ekpo-ebelp
      AND menge <> wemng.

ABAP thinks wemng is supposed to be a variable.

How can I do it?

Upvotes: 1

Views: 1047

Answers (2)

st4hoo
st4hoo

Reputation: 2204

You should use column selector(~) when you compare two columns in WHERE condition. This is by design.

ABAPDOCU says:

Column selector

Character ~. A column (col) of a database table (dbtab) can be addressed in a SELECT statement by means of dbtab~col. This type of addressing is necessary when multiple database tables are accessed if the name of a column occurs in a number of different database tables or if two columns are compared with each other in a comparison in the WHERE condition.

So, in your case:

SELECT menge wemng ebeln ebelp
    INTO (eket-menge,
          eket-wemng,
          eket-ebeln,
          eket-ebelp)
    FROM eket
    WHERE ebeln = ekpo-ebeln
      AND ebelp = ekpo-ebelp
      AND menge <> eket~wemng.
...

Upvotes: 3

Gustawson
Gustawson

Reputation: 33

Please check below query:

  SELECT menge wemng ebeln ebelp
  INTO (eket-menge,
        eket-wemng,
        eket-ebeln,
        eket-ebelp)
  FROM eket
  WHERE ebeln = ekpo-ebeln
    AND ebelp = ekpo-ebelp
    AND menge <> eket~wemng.

But at this moment you get error:

Incorrect nesting: Before the statement "ENDIF", the control structure introduced by "SELECT" must be concluded with "ENDSELECT".

So you should create some internal table to handle this

  SELECT menge wemng ebeln ebelp
  INTO TABLE lt_eket
  FROM eket
  WHERE ebeln = ekpo-ebeln
    AND ebelp = ekpo-ebelp
    AND menge <> eket~wemng.

or use SELECT SINGLE

  SELECT SINGLE menge wemng ebeln ebelp
  INTO (eket-menge,
        eket-wemng,
        eket-ebeln,
        eket-ebelp)
  FROM eket
  WHERE ebeln = ekpo-ebeln
    AND ebelp = ekpo-ebelp
    AND menge <> eket~wemng.

Upvotes: 2

Related Questions