Search code examples
abapopensql

Put database field in the RHS on the WHERE condition?


How we can write the below WHERE?

SELECT * FROM acdoca
WHERE koart = @(COND #( blart = 'DY' THEN 'D' ELSE 'S' ) )

It does not recognize the field name BLART. How can we pass it?
Thanks
Elias

EDIT: here is the whole WHERE:

WHERE acdoca~rldnr = '0L' AND
      acdoca~rbukrs = @company_code AND
      acdoca~belnr IN @document_no_range AND
      acdoca~gjahr = @year AND
      acdoca~koart = @(COND #( blart = 'DY' THEN 'D' ELSE 'S' ) )

Solution

  • ​A Host Expression @( ... ) is only interpreted at ABAP side, there is absolutely no interaction with the database server, the expression is evaluated and the SQL statement is updated with the result before it is sent to the database.

    If I understand well what you want to achieve, this should work:

      SELECT * FROM acdoca
          WHERE ( blart = 'DY' and koart = 'D' )
             OR ( blart <> 'DY' and koart = 'S' )
          INTO TABLE @DATA(itab).
    

    NB: if I didn't understand well and you really want a Host Expression, a valid syntax would be this one:

    DATA blart TYPE acdoca-blart.
    SELECT * FROM acdoca
          WHERE koart = @( COND #( WHEN blart = 'DY' THEN 'D' ELSE 'S' ) )
          INTO TABLE @DATA(itab).
    

    EDIT: here is the complement to the EDIT of the question, the whole correct WHERE would be:

    WHERE acdoca~rldnr = '0L' AND
          acdoca~rbukrs = @company_code AND
          acdoca~belnr IN @document_no_range AND
          acdoca~gjahr = @year AND
      ( ( acdoca~blart <> 'DY' AND acdoca~koart = 'S' ) OR
        ( acdoca~blart = 'DY' AND acdoca~koart = 'D' ) )