1. 创建一个Core Data Service Table Functions
新建 Core Data Service Table Function
-
定义CDS Table Functions
@EndUserText.label: 'a simple AMDP for CDS Table Functions'
@ClientDependent: true //打开 Open SQL 的自动客户端处理
define table function ZAMDP_CDS_TABFUNC_01
with parameters @Environment.systemField: #CLIENT //Open SQL 通过将sy-mandt的值隐式传递给该参数来处理
ip_clnt:abap.clnt, //入参client
ip_matnr:matnr
returns {
MANDT:abap.clnt; //Field:ref elements
MATNR:matnr;
ERSDA:ersda;
ERNAM:ernam;
MTART:mtart;
MATKL:matkl;
SPRAS:spras;
MAKTX:maktx;
}
implemented by method zamdp_demo002=>GET_MARA;
2. 实现CDS Table Functions中的AMDP Class
- 实现ABAP Class
CLASS zamdp_demo002 DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
interfaces: IF_AMDP_MARKER_HDB.
class-methods:
GET_MARA
for table function zamdp_cds_tabfunc_01.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zamdp_demo002 IMPLEMENTATION.
method GET_MARA by database function
for hdb language sqlscript
options read-only
using mara makt.
*可以多表连接 也可以单表查询
return
with makt_tmp as ( select * from makt )
select t1.MANDT,
t1.MATNR,
t1.ERSDA,
t1.ERNAM,
t1.MTART,
t1.MATKL,
t2.SPRAS,
t2.MAKTX
from mara as t1
inner join makt_tmp as t2 on t1.matnr = t2.matnr
where t1.mandt = :ip_clnt --Use Table Function Parameter
and t1.MATNR = :ip_matnr;
endmethod.
ENDCLASS.
3. ABAP中调用AMDP Functions for CDS Table Functions
*&---------------------------------------------------------------------*
*& Report ZTMP_TEST_IT029
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTMP_TEST_IT029.
START-OF-SELECTION.
SELECT *
FROM zamdp_cds_tabfunc_01( ip_matnr = '000000000100000001' )
INTO TABLE @DATA(result) ##db_feature_mode[amdp_table_function].
cl_demo_output=>display( result ).