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.
Who needs helper variables in 740?
It seems with 7.4 ABAP is becoming a language worth working with, and funny too.
Thanks Horst, I keep following you!
hmmm...
can there be a typo?
DATA name TYPE scarr-carrname.
result->Get_param( REF #( name ) ). "instead of set_param( )
result->next( ).
No typo ..., see http://help.sap.com/abapdocu_740/en/abenadbc_query.htm
Horst,
when executing this line
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
all runs well. Though the table is there and a single record gets fetched.
cu,
Michael
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
?
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
Not strange, but native ...