Skip to Content
Author's profile photo Horst Keller

ABAP News for Release 7.40 – Constructor Operator REF

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.

Assigned Tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Who needs helper variables in 740?

      Author's profile photo Paolo Romano
      Paolo Romano

      It seems with 7.4 ABAP is becoming a language worth working with, and funny too.

       

      Thanks Horst, I keep following you!

      Author's profile photo Tom Van Doorslaer
      Tom Van Doorslaer

      hmmm...

      can there be a typo?

       

      DATA name TYPE scarr-carrname.

      result->Get_param( REF #( name ) ). "instead of set_param( )

      result->next( ).

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author
      Author's profile photo Michael Fritz
      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

      Author's profile photo Horst Keller
      Horst Keller
      Blog 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 = ?` ).

      ?

      Author's profile photo Michael Fritz
      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

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      Not strange, but native ...