S4HANA For ABAP(5):CDS View

1.22 CDS VIEW: Core Data Services

CDS View是SAP提供的新的DDL Source数据字典对象,将应用层复杂逻辑下沉到数据库层;

CDS View只能使用ABAP Development Tools in Eclipse进行开发;

S4HANA For ABAP(5):CDS View_第1张图片

1.选择Package,右键->New->Other ABAP Repository Object;

2.有多种Template选择;

S4HANA For ABAP(5):CDS View_第2张图片

3.定义CDS View;

示例1:

1.如果需要重命名View,需要查找Table:VRSD,删除objname = 'view name';

[email protected]:指定Dictionary中View名称;

[email protected]:指定View的描述;

//重命名view,删除Table:VRSD,objname = 'view name

@AbapCatalog.sqlViewName: 'ZTOM_CDS_SPFLI1'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'test cds view'

define view ZTOM_CDS_SPFLI as select from spfli {

    key carrid,

    key connid,

    countryfr,

    cityfrom,

    airpfrom,

    countryto,

    cityto,

    airpto,

    fltime,

    deptime,

    arrtime,

    distance,

    distid,

    fltype,

    period

}

S4HANA For ABAP(5):CDS View_第3张图片

4.通过SAP SE11查看View;

示例2:另一种格式

1.define view:View Name不能和@AbapCatalog.sqlViewName重复;

[email protected]最大长度16;

@AbapCatalog.sqlViewName: 'ZTOM_CDS_SPFLI_2'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'test cds view2'

define view ZTOM_CDS_SPFLI2 as select

carrid,

connid,

cityfrom,

cityto

from spfli


示例3:

1.可以使用Join进行多表连接;

[email protected],不能指定字段描述,如果需要字段描述,需要定义data type指定字段data type,View自动带出data type描述;

3.通过cast( )进行类型转换;

@AbapCatalog.sqlViewName: 'ZTOM_CDS_CAL1'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'test cds view calculate column'

define view ZTOM_CDS_1 as select from spfli

left outer join scarr

on scarr.carrid = spfli.carrid {

    key spfli.carrid,

    key spfli.connid,

    spfli.countryfr,

    spfli.countryto,

    scarr.carrname,

    scarr.url,

    @EndUserText.label: 'Total Distance'

    cast(sum(spfli.distance) as abap.dec(15,2)) as total_distance,

    @EndUserText.label: 'Total Flytime'

    cast(sum(spfli.fltime) as abap.dec(15,3)) as total_fltime,

    @EndUserText.label: 'Avg Distance'

    avg(spfli.distance) as avg_distance,

    --计算类型quan不能使用类型转换?

    --avg(spfli.distance as abap.dec(15,2)) as avg_distance,

    @EndUserText.label: 'Avg Flytime'

    avg(spfli.fltime as abap.dec(15,2)) as avg_fltime,

    //使用自定义data type,使用data type描述

    cast(sum(spfli.distance) as zt_dec15 ) as total_distance1

} group by spfli.carrid,spfli.connid,spfli.countryfr,spfli.countryto,scarr.carrname,scarr.url

示例4:

1.fltp_to_decFLTP类型转换为DEC类型;

@AbapCatalog.sqlViewName: 'ZTOM_CDS_CAL2'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'tom test cds fltp to dec'

define view ZTOM_CDS_2 as select from ztom_cds_cal1 {

    key carrid,

    key connid,

    --通过fltp_to_dec转换

    @EndUserText.label: 'Avg Distance'

    fltp_to_dec(avg_distance as abap.dec(15,2)) as avg_distance_dec

}

1.23 CDS VIEW:Annotations

1.Annotations類似註解,對CDS View一些參數進行控制;

2.使用“@”開頭;

3.可以自定義Annotations;

4.很多Annotation都是針對不同平台或框架使用,比如Query,OData, SAP Fiori UI等;

View Annotation:

@ClientDependent:True,打開或關閉Client控制;

@AbapCatalog.Buffering.type:#FULL,設置SAP Table Buffer;

@AbapCatalog.Buffering.status:#ACTIVE,設置SAP Table Buffer;

@EndUserText.label:'Table Description',設置View描述;

@Analytics.query:true,View Define for Query;

@OData.publish:true,Odata Service is generated for this view;

@AccessControl.authorizationCheck:#CHECK,權限檢查;

@VDM.viewType:#CONSUMPTIOM,Virtual Data Model種類;

Fields Annotation:

@EndUserText.label:'Table Description',設置字段描述,放在字段前一行;

@

@AnalyticsDetails.query.axis:#ROWS,設置字段在Query中作為行;

@AnalyticsDetails.query.axis:#COLUMNS,設置字段在Query中作為列;

@DefaultAggregation:#SUM,設置字段加總方式;

1.24 CDS VIEW: Using Expressions and Functions

CDS View中提供類似Open SQL的Expressions,Functions可以根據需求處理數據字段;

示例1:

1.CDS View字段算術運算,數據類型;

2.CDS View字段字符串處理;

3.CASE WHEN語法;

@AbapCatalog.sqlViewName: 'ZTOM_CDS_CAL3'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'test cds view expressions and functions'

define view ZTOM_CDS_3 as select from sflight {

  //算术运算

  // +,-,*,/

  // Number Function:ABS,CEIL,FLOOR,DIV,MOD,ROUND

  // /只能在FLTP类型之间使用

  // DIV,MOD只能处理整数类型

  // 數據類型:

  //abap.char(len),abap.clnt,

  //abap.cuky(len),abap.curr(len,decimals),

  //abap.dats,abap.dec(len,decimals),

  //abap.fltp,abap.int1/2/4/8,

  //abap.lang,abap.numc(len),abap.quan(len,decimals),

  //abap.sstring(len),abap.tims,abap.unit(len)

  key carrid,

  key connid,

  key fldate,

  price * seatsmax as amt_sum,

  seatsmax - seatsocc as seats_remain,

  seatsocc_b + seatsocc_f as seats_occ,

  cast( paymentsum as abap.fltp ) /

  cast( seatsocc as abap.fltp ) as func_price,

  ABS( price ) as func_abs,

  CEIL( price ) as func_ceil,

  FLOOR( price ) as func_floor,

  DIV( seatsocc,seatsmax ) as func_div,

  MOD( seatsocc,seatsmax ) as func_mod,

  ROUND( price,0 ) as func_round,

  //字符运算

  //&&,字符串连接,可以任意数量字符串连接(CDS不支持)

  //結果最长255,只能是字符类型

  //CONCAT,字符串连接,只能两个字符串连接

  //结果最长1333,可以是字符类型字段,function,string

  //LPAD,左边字符传截取

  //LENGTH,字符长度

  //LTRIM,左边字符删除

  //REPLACE,字符串替换

  //RIGHT,右边字符串截取

  //RTRIM,右边字符删除

  //SUBSTRING,字符串截取

  //'Plane Type:' && planetype && '-END' AS plane_type,

  CONCAT( 'Plane Type:',planetype ) as plane_type1,

  LPAD( 'HELLO',4,'H' ) as func_lpad,//HELL

  LENGTH( planetype ) as func_length,

  LTRIM( 'HELLO','H' ) as func_ltrim, //ELLO

  REPLACE( 'HELLO','L','@' ) as func_replace, //HE@@O

  RIGHT( 'HELLO',3 ) as func_right, //LLO

  RTRIM( 'HELLO','O' ) as func_rtrim, //HELL

  SUBSTRING( 'HELLO',1,3 ) as func_substring, //HEL

 

  //case when,條件不支持計算表達式

  case when seatsocc > 200 then 'Seats Low'

       when seatsocc < 0 then 'Seats Error'

  else 'Seats Full'

  end as case_status

 

}

示例2:

1.使用Unit_Conversion單位轉換function;

2.dats_days_bwtween兩個日期之差;

3.dats_add_days,日期計算;

4.dats_tims_to_tstmp,日期時間轉換為timestamp;

5.tstmp_current_utctimestamp(),當前timestamp;

@AbapCatalog.sqlViewName: 'ZTOM_CDS_CAL4'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'test cds view date function'

define view ZTOM_CDS_4 as select from spfli {

    key carrid,

    key connid,

    cityfrom,

    cityto,

    @Semantics.quantity.unitOfMeasure: 'DISTID'

    distance,

    @Semantics.unitOfMeasure: true

    distid,

    Unit_Conversion(

        quantity => distance,

        source_unit => distid,

        target_unit => cast( 'MI' as abap.unit )

    ) as conv_distance,

    //日期function

    dats_days_between(cast('20220101' as abap.dats),

    cast('20221020' as abap.dats))

    as func_day_between,

    //‘FAIL’表示如果失敗,報錯

    dats_add_days(cast('20220101' as abap.dats),

    10,

    'FAIL') as func_day_add,

    //日期,時間轉換為timestamp類型

    dats_tims_to_tstmp(cast('20220101' as abap.dats),

    deptime,

    abap_system_timezone($session.client,'NULL'),

    $session.client,

    'NULL') as func_day_ts,

    //獲取系統日期

    $session.system_date as sys_date,

    //當前timestamp

    tstmp_current_utctimestamp() as  func_current_ts,

    tstmp_seconds_between(

    tstmp_current_utctimestamp(),

    tstmp_current_utctimestamp() - 1,

    'FAIL') as func_tstmp_between

}

1.25 CDS VIEW: Joins and Associations

CDS VIEW提供更多Join方式,還可以定義association進行多表操作;

示例1:

1.使用right outer join;

2.使用left outer join;

3.使用inner join;

4.使用union;

@AbapCatalog.sqlViewName: 'ZTOM_CDS_CAL5'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'test cds view join,assosiation'

define view ZTOM_CDS_5

as select from spfli as a

right outer join scarr as b

on a.carrid = b.carrid

and b.carrid = 'AZ'

{

    a.carrid,

    a.connid,

    b.carrname,

    b.url,

    a.cityto,

    'A' as flag

}

where ( a.carrid = 'AZ' or a.carrid = 'AA')

and ( a.fltype <> 'X'

   or a.period is not null )

union

select from spfli as a

left outer join scarr as b

on a.carrid = b.carrid

and b.carrid = 'AZ'

{

    a.carrid,

    a.connid,

    b.carrname,

    b.url,

    a.cityto,

    'B' as flag

}where ( a.carrid = 'AZ' or a.carrid = 'AA')

union

select from spfli as a

inner join scarr as b

on a.carrid = b.carrid

and b.carrid = 'AZ'

{

    a.carrid,

    a.connid,

    b.carrname,

    b.url,

    a.cityto,

    'C' as flag

}where ( a.carrid = 'AZ' or a.carrid = 'AA')

示例2:

1.使用association,定義table間關係;

[1]:1對1

[0..1]:0對1

[0..*]:0對多

[1..*]:1對多

@AbapCatalog.sqlViewName: 'ZTOM_CDS_CAL6'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'test cds view association'

define view ZTOM_CDS_6

as select from scarr as a

association[1] to tcurt as b

on a.currcode = b.waers

and b.spras = 'E'

{

    key a.carrid,

    key 'A' as flag,

    a.carrname,

    a.currcode,

    b.spras,

    b.ltext,

    b.ktext

}

union

select from scarr as a

association[0..*] to tcurt as b

on a.currcode = b.waers

{

    key a.carrid,

    key 'B' as flag,

    a.carrname,

    a.currcode,

    b.spras,

    b.ltext,

    b.ktext   

}

示例3:

1.定義多個association,可以定義association別名,在其他CDS View中通過別名訪問;

@AbapCatalog.sqlViewName: 'ZTOM_CDS_CAL7'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'test cds view association table'

define view ZTOM_CDS_7

as select from spfli as a

association [1] to scarr as carr_info

on a.carrid = carr_info.carrid

//相當於left outer join

association [1..*] to sflight as b

on a.carrid = b.carrid

and a.connid = b.connid

and a.carrid = 'AA'

{

    a.carrid,

    carr_info,

    a.connid,

    a.cityto,

    b.fldate,

    b.planetype,

    b.seatsmax  

}
@AbapCatalog.sqlViewName: 'ZTOM_CDS_CAL8'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'test cds view association use association table'

define view ZTOM_CDS_8

//這裡需要使用CDS View名稱

as select from ZTOM_CDS_7

{

    key carrid,

    key connid,

    carr_info.carrname,

    carr_info.url,

    cityto,

    seatsmax

}

示例4:

1.使用association filter

define view ZTOM_CDS_61

as select from scarr as a

association[0..*] to tcurt as b

on a.currcode = b.waers

{

    key a.carrid,

    a.carrname,

    a.currcode,

    b.spras,

    b.ltext,

    b.ktext,

    //使用association filter

    b[1:spras='E'] as txt_en,

    b[1:spras='D'] as txt_de

}

你可能感兴趣的:(ABAP,S4HANA,abap)