Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  AMDP - ABAP Managed Database Procedures →  AMDP - Methods 

[CLASS-]METHODS - AMDP OPTIONS

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


[CLASS-]METHODS meth ... AMDP OPTIONS [READ-ONLY] 
                                      [CDS SESSION CLIENT clnt|CURRENT] ...

Extras

1. ... READ-ONLY

2. ... CDS SESSION CLIENT clnt|CURRENT

Effect

The addition AMDP OPTIONS for METHODS and CLASS-METHODS statements can be used to declare attributes of AMDP methods of global classes or interfaces. After AMDP OPTIONS, at least one attribute must be specified.

If the addition AMDP OPTIONS is used in the declaration of a method, but the method is implemented as a normal ABAP method without the addition BY DATABASE PROCEDURE|FUNCTION, the attributes specified byAMDP OPTIONS are ignored. However, the following restrictions for AMDP methods apply to the method declaration:

A prerequisite for using this addition is that the global class or the interface must contain a tag interface IF_AMDP_MARKER_... for AMDP classes. AMDP OPTIONS cannot be used in local classes, for the constructors constructor or class_constructor, for event handlers declared with FOR EVENT, for redefinitions declared with REDEFINITION, or for AMDP function implementations for CDS table functions declared with FOR TABLE FUNCTION.

Addition 1

... READ-ONLY

Effect

If the option READ-ONLY is specified, only reads of database tables are allowed in the implementation of the database procedure or database function. Only database procedures or database functions of other AMDP methods that are also marked as READ-ONLY can be called. This is checked by the syntax check or at runtime.

This attribute can also be specified using the addition OPTIONS in the implementation of an AMDP method with METHOD meth BY DATABASE PROCEDURE|FUNCTION. It applies when specified either in the declaration or in the implementation of the method, or both.

The option READ-ONLY must be specified at least once in the implementation of an AMDP function or an L procedure. If the addition AMDP OPTIONS is used in the declaration of a method with a RETURNING parameter, the option READ-ONLY must already be specified in the declaration.

Addition 2

... CDS SESSION CLIENT clnt|CURRENT

Effect

If the option CDS SESSION CLIENT is used, the session variable of the database that can be addressed in the CDS DDL of the ABAP CDS under the name $session.client is supplied with a value when the AMDP method is called from ABAP. In the SAP HANA database this is the ABAP-specific session variable CDS_CLIENT. The value is determined as follows:

  • The name of an input parameter of the current method can be specified for clnt. Its type must be compatible with the built in type CLNT in ABAP Dictionary. The session variable is set to the value passed to this parameter during the call, or to the value of the replacement parameter declared with DEFAULT.
  • If CURRENT is specified, the session variable is set to its default value, that is, to the nominal value of the ABAP system field sy-mandt or the client of the current logon. In this case there can be no input parameters with the name current.

The session variable is only set when the AMDP method is called from ABAP. If the associated database procedure or function is called from another AMDP method or from a database procedure or function that is not managed by AMDP, the session variable is not affected.

If the database procedure or function of an AMDP method that is declared with the option CDS SESSION CLIENT is called from an AMDP procedure or function implementation or during a SELECT statement, the following rules apply:

  • AMDP method as call target
  • The implementation of an AMDP method that is declared with the addition CURRENT can only be called in implementations of methods with the addition CURRENT, and not during the execution of a SELECT statement with the addition USING CLIENT.
  • The implementation of an AMDP method declared with clnt can only be called in implementations of methods where the addition CURRENT or clnt is specified, and during the execution of a SELECT statement with the addition USING CLIENT.
  • The implementation of an AMDP method that is declared without any additions can be called from any implementations and during the execution of any SELECT statements.
  • AMDP method as caller
  • The implementation of an AMDP method that is declared with the addition CURRENT can call any implementations.
  • The implementation of an AMDP method declared with clnt cannot call any implementations of methods where CURRENT is specified.
  • The implementation of an AMDP method that is declared without any additions can only call other implementations without additions.

A call can occur during a SELECT statement if it directly or indirectly accesses a CDS table function, because this is implemented as an AMDP table function.

If an AMDP method accesses the CDS database view of a CDS view whose client handling is defined by the annotation @ClientHandling.algorithm:

SESSION_VARIABLE, it must be declared with the option CDS SESSION CLIENT. If not, a syntax error occurs.


Notes

  • The option CDS SESSION CLIENT is mainly required if an AMDP method accesses the CDS database view of a CDS view whose client handling is determined by the annotation @ClientHandling.algorithm: #SESSION_VARIABLE. In database views like this, the session variable associated with $session.client is usually evaluated in comparisons with a client column. If the option CDS SESSION CLIENT is not used when calling AMDP methods (or the addition USING CLIENT in ABAP SQL), the default value for the session variable would be the nominal value of the ABAP system field sy-mandt. If the session variable is set when an AMDP method is called, the CDS database views that are used select the data of the required client. If, in the implementation of the AMDP method, the client ID that is selected when the CDS database view is accessed does not match the value of the session variable, the results set is empty.
  • If CDS SESSION CLIENT is used for an AMDP method, the AMDP framework sets the session variable belonging to $session.client exactly once when this method is called from ABAP. The following recommendations apply:

  • The value of the input parameter marked with clnt should be treated as a constant and should not be changed when executing the implementation, so that it always matches the value of the session variable. When calling other implementations of AMDP methods that are declared with CDS SESSION CLIENT, only the client ID that matches the value of the session variable should be passed.

  • The value of the session variable should not be overwritten using database methods.
Whether the input parameter is used explicitly in self-programmed conditions or implicitly in CDS database views, this ensures that the data of the same client is accessed.
  • The rules governing how AMDP methods declared with the option CDS SESSION CLIENT call each other support the previous point:
  • In a call chain that leads to a method with the addition CURRENT, the session variable that belongs to $session.client can only have the nominal value of the ABAP system field sy-mandt.
  • In a call chain that leads to a method that specifies clnt, the session variable that belongs to $session.client can have a value defined explicitly by clnt.


Example

The following implementation of an AMDP method accesses the CDS database view DEMO_CDS_PRJCT0A of a client-specific CDS view:

METHOD get_spfli_view BY DATABASE PROCEDURE FOR HDB
                      LANGUAGE SQLSCRIPT
                      USING demo_cds_prjct0a.
  connections  = select *
                        from DEMO_CDS_PRJCT0A;
ENDMETHOD.

The DDL source code used to define the actual CDS entity DEMO_CDS_SPFLI_CLIENT_0A is as follows:

@AbapCatalog.sqlViewName: 'DEMO_CDS_PRJCT0A'
@AccessControl.authorizationCheck: #NOT_ALLOWED
@ClientHandling.type: #CLIENT_DEPENDENT
@ClientHandling.algorithm: #SESSION_VARIABLE
define view demo_cds_spfli_client_0a
  as select from
    spfli
    {
      key spfli.carrid,
      key spfli.connid,
          spfli.cityfrom,
          spfli.cityto
    }

The client handling is defined using the annotation @ClientHandling.algorithm:

SESSION_VARIABLE. For this reason, the version of the view in the database contains the following WHERE condition (in this case for the SAP HANA database):

WHERE ( "SPFLI"."MANDT" = SESSION_CONTEXT( 'CDS_CLIENT') )

This WHERE condition applies each time the view is accessed. If the SELECT statement in the above AMDP method had its own WHERE condition for the client column MANDT and this selected a client other than the session variable CDS_CLIENT, the results set would be empty. For this reason, accessing such a CDS database view in an AMDP method causes the syntax check to issue a warning by default. This warning can be bypassed by using the addition AMDP OPTIONS CDS SESSION CLIENT when declaring the method in the class CL_DEMO_AMDP_SESSION_CLIENT:

CLASS cl_demo_amdp_session_client DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_amdp_marker_hdb .
    TYPES t_connections TYPE STANDARD TABLE OF demo_cds_prjct0a
                        WITH EMPTY KEY.
    METHODS get_spfli_view
      AMDP OPTIONS READ-ONLY
                   CDS SESSION CLIENT clnt
      IMPORTING VALUE(clnt)        TYPE sy-mandt
      EXPORTING VALUE(connections) TYPE t_connections
      RAISING   cx_amdp_error.

When the method is called from ABAP, the session variable CDS_CLIENT is set to the value passed to the input parameter clnt. The program DEMO_AMDP_SESSION_CLIENT calls the AMDP method and it is possible to enter different values for the client ID. The data of the client in question is selected.