Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  Native SQL →  EXEC SQL - Embedded Native SQL →  EXEC SQL - Examples 

EXEC SQL, Accessing a Database Function

This example demonstrates how a database function is accessed using static Native SQL.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    TRY.
        EXEC SQL.
          DROP FUNCTION abap_docu_demo_scalar_function;
        ENDEXEC.
        EXEC SQL.
          CREATE FUNCTION
            abap_docu_demo_scalar_function
              ( IN p1 NVARCHAR(5), IN p2 NVARCHAR(5) )
              RETURNS r NVARCHAR(10) AS
              BEGIN
                r = concat(p1,p2);
              END;
        ENDEXEC.
      CATCH cx_sy_native_sql_error INTO DATA(exc).
        cl_demo_output=>display( exc->get_text( ) ).
        RETURN.
    ENDTRY.

    DELETE FROM demo_expressions.
    INSERT demo_expressions
      FROM TABLE @( VALUE #( ( id = 'X' char1 = 'xxxxxaaaaa' )
                             ( id = 'Y' char1 = 'yyyyybbbbb' )
                             ( id = 'Z' char1 = 'zzzzzccccc' ) ) ).

    DATA in1 TYPE c LENGTH 5 VALUE 'yyyyy'.
    DATA in2 TYPE c LENGTH 5 VALUE 'bbbbb'.
    cl_demo_input=>new(
      )->add_field( CHANGING field = in1
      )->add_field( CHANGING field = in2 )->request( ).

    DATA result TYPE demo_expressions-id.
    TRY.
        EXEC SQL.
          select id
                 from demo_expressions
                 where char1 = abap_docu_demo_scalar_function(:in1,
                                                            :in2)
                 into :result
        ENDEXEC.
      CATCH cx_sy_native_sql_error INTO exc.
        cl_demo_output=>write( exc->get_text( ) ).
    ENDTRY.

    cl_demo_output=>display( result ).

Description

After EXEC SQL, this example creates a scalar database function with two input parameters and uses this function in the WHERE condition of a SELECT statement.