Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Read Accesses →  SELECT →  SELECT - result →  SELECT - select_list →  SELECT - col_spec →  SELECT - sql_exp →  Examples of SQL Expressions 

SQL Expressions, Simple CASE and &&

This example demonstrates simple case distinctions and chainings in SQL expressions.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA itab TYPE TABLE OF demo_expressions WITH EMPTY KEY.

    itab = VALUE #(
      ( id = 'x' char1 = 'aaaaa' char2 = 'bbbbb' )
      ( id = 'y' char1 = 'xxxxx' char2 = 'yyyyy' )
      ( id = 'z' char1 = 'mmmmm' char2 = 'nnnnn' ) ).

    DELETE FROM demo_expressions.
    INSERT demo_expressions FROM TABLE @itab.

    DATA else TYPE c LENGTH 10 VALUE 'fffff'.
    SELECT id, char1, char2,
           CASE char1
             WHEN 'aaaaa' THEN ( char1 && char2 )
             WHEN 'xxxxx' THEN ( char2 && char1 )
             ELSE @else
           END AS text
           FROM demo_expressions
           INTO TABLE @DATA(results).

    cl_demo_output=>display( results ).

Description

In a list of columns specified after SELECT, CASE is used to make a simple case distinction for the content of a column. The result is a character string chained from column content or taken from a host variable. Using the alternative column name defined after AS, the result is assigned to the column with the same name in an internal table declared inline, results.