今天做一个ABAP CDS 中Table Function的Demo
1、定义Table Function
@EndUserText.label: 'Demo tab function'
define table function ZDEMO_TAB_FUNC
with parameters
// @Environment.systemField: #CLIENT
// iv_mandt : abap.clnt, // 参数 client 可以不用指定
iv_CARRID : s_carr_id
returns
{
mandt : s_mandt; // 跨client表 必须输出字段
CARRID : s_carr_id;
CONNID : s_conn_id;
FLDATE : s_date;
PRICE : s_price;
CURRENCY : s_currcode;
PLANETYPE : s_planetye;
SEATSMAX : s_seatsmax;
SEATSOCC : s_seatsocc;
}
implemented by method
zcl_demo_tab_func=>get_tab_func;
2、在ADMP类中实现Table Function的取数逻辑
CLASS zcl_demo_tab_func DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.
"!定义实现TABLE FUNCTION的方法时 必须制定 table function的name
CLASS-METHODS get_tab_func FOR TABLE FUNCTION zdemo_tab_func.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_demo_tab_func IMPLEMENTATION.
METHOD get_tab_func BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY USING sflight.
-- 和普通的 amdp所不同的是,必须使用return 语句返回 与table function相同字典结构的结果集
return select MANDT,
CARRID,
CONNID,
FLDATE,
PRICE,
CURRENCY,
PLANETYPE,
SEATSMAX,
SEATSOCC
from sflight
where carrid = :iv_carrid;
ENDMETHOD.
ENDCLASS.
3、在程序中使用Table Function
REPORT zdemo_tab_func.
*注意 table function的参数的传递方式, table function 可以当做表来使用
SELECT * FROM zdemo_tab_func( IV_CARRID = 'AA' )
WHERE CONNID IN ( '0017','0064' )
INTO TABLE @DATA(lt_tab).
CL_DEMO_OUTPUT=>display(
EXPORTING
data = LT_TAB " Text or Data
).