Skip to Content

With Release 7.40 ABAP supports so called constructor operators. Constructor operators are used in constructor expressions to create a result that can be used at operand positions. The syntax for constructor expressions is

… operator type( … ) …

operator is a constructor operator. type is either the explicit name of a data type or the character #. With # the data type can be dreived from the operand position if the operand type is statically known. Inside the parentheses specific parameters can be specified.

Reference Operator REF

This is a short one but can become handy for some purposes.

The reference operator REF constructs a data reference at operand positions.

… REF dtype|#( dobj ) …

results in a data reference pointing to dobj with the static type specified by type. With other words, REF is the short form for GET REFERENCE OF dobj INTO.

Where do you need it? Always when you have to pass data references to somewhere and don’t want to create helper variables for that purpose.

Example for dynamic method call

Using REF for filling the parameter table:

CLASS class DEFINITION.
  PUBLIC SECTION.
    METHODS meth
      IMPORTING p1 TYPE string
                p2 TYPE i.
ENDCLASS.

CLASS class IMPLEMENTATION.
  METHOD meth.
    …
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

  DATA(arg1) = `blah`.
  DATA(arg2) = 111.

  DATA(ptab) = VALUE abap_parmbind_tab(
    ( name = ‘P1’ kind = cl_abap_objectdescr=>exporting value = REF #( arg1 ) )
    ( name = ‘P2’ kind = cl_abap_objectdescr=>exporting value = REF #( arg2 ) ) ).

  DATA(oref) = NEW class( ).
  CALL METHOD oref->(‘METH’)
    PARAMETER-TABLE ptab.

Example for ADBC

Parameter binding made easy!

DATA(key) = ‘LH’.

DATA(sql) = NEW cl_sql_statement( ).

sql->set_param( REF #( sy-mandt ) ).
sql->set_param( REF #( key ) ).

DATA(result) = sql->execute_query(
      `SELECT carrname ` &&
      `FROM scarr ` &&
      `WHERE mandt  = ? AND carrid = ?` ).

DATA name TYPE scarr-carrname.
result->set_param( REF #( name ) ).
result->next( ).

Believe it or not: name contains the carrier name now.

To report this post you need to login first.

8 Comments

You must be Logged on to comment or reply to a post.

  1. Michael Fritz

    Horst,

     

    when executing this line

     

    DATA(result) = sql->execute_query(

          `SELECT carrname ` &&

          `FROM scarr ` &&

          `WHERE mandt  = ? AND carrid = ?` ).

     

    my sample program throws an exception of type cx_sql_exception telling me “Invalid object name ‘scarr’“.

     

     

    Do you know by any chance what might be the cause of this?

     

    OTH, when using

    SELECT SINGLE carrname

    INTO name

    FROM scarr

    WHERE carrid = ‘LH’.

     

    all runs well. Though the table is there and a single record gets fetched.

     

    cu,

    Michael

    (0) 
    1. Horst Keller Post author

      Hi,

       

      what’s your DB system?

       

      It is running fine on HANA and MaxDB.

       

      Maybe the SQL of your DB is case sensitive and you must write

       

      DATA(result) = sql->execute_query(

            `SELECT CARRNAME ` &&

            `FROM SCARR ` &&

            `WHERE MANDT  = ? AND CARRID = ?` ).

      ?

      (0) 
      1. Michael Fritz

        Horst,

         

        thanks for your reply!

         

        We are running a MS SQL 11.00.5548.00 (guess it’s an SQL Server 2012) and as far as I know this one is not case sensitive.

        Anyway, I’ve tried your suggestion and changed all field names and table names into uppercase and all runs well now.

         

        Strange things happen here…

         

        cu,

        Michael

        (0) 

Leave a Reply