SAP CDS View基礎語法(創建你的第一個CDS View)

很多同學對於CDS view感覺無從下手,本篇博客將介紹CDS View的基礎語法,並附有示例。

1. 定義一個CDS View

用途:

創建一個CDS View

語法:

@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
define view ZDEMO_CDS_DDL
  as select from sbook
{
  carrid,
  connid
}

解釋:

  • @AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL' 指定了數據庫SQL View的名字,在CDS View激活時,會在數據庫層生成對應的SQL View
  • define view ZDEMO_CDS_DDL是定義的CDS View的名字
  • as select from sbook指定了CDS View的數據源,此處的數據源是DB Table sbook
  • { }中定義了CDS view中包含哪些字段

運行效果:

2. Select Distinct 

用途:

如果結果集中有重複的條目,DISTINCt可排除結果集中的重複條目。

語法:

@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
define view ZDEMO_CDS_DDL
  as select distinct from sbook
{
  carrid,
  connid
}

解釋:

  • as select distinct from sbook指定了CDS View的數據源,此處的數據源是DB Table sbook

運行效果:

3. Where語句

用途:

限定篩選條件

語法:

@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
define view ZDEMO_CDS_DDL
  as select from sbook
{
  carrid,
  connid
}
where carrid = 'AA'

運行效果:

4. Key 字段聲明

用途:

指定CDS View中哪些字段是Key字段

語法:

@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
  as select from sbook
{
  key carrid,
  key connid,
      fldate,
      bookid,
      customid,
      custtype
}

解釋:

  • @AbapCatalog.preserveKey: true 這句annotation 的值爲TRUE時,SQL view中的key字段使用CDS中定義的key; 值爲FALSE時,使用DB table中table的key field.

運行效果:

5. Build-in 函數

用途:

CDS中自帶的一些運算函數

語法:

@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
  as select from sbook
{
  key carrid,
  key connid,
      concat( carrid, connid ) as flightno,
      order_date,
      concat( concat (substring(order_date,5,2),'-'),
      substring(order_date,1,4) ) as ordermonth
}

解釋:

  • concat () 拼接兩個字符串,substring( )獲取字符串中的一個子串。
  • 更多可用的Build-in函數可以查找ABAP的幫助文檔

運行效果:

6. CASE表達式

用途:

實現分支運算

語法:

@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
  as select from sbook
{
  key carrid,
  key connid,
      case carrid
            when 'AA' then 'American Airlines'
            when 'AB' then 'Air Berlin'
            when 'UA' then 'United Airlines'
            else 'Other Airlines'
            end as airline_name,
      class,
      case
         when class = 'F' then 'First Class'
         when class = 'Y' then 'Business Class'
         else 'others'
         end    as class_level
}

解釋:

  • case...when...else...end as 構成分支運算的邏輯
  • when語句中可以是邏輯表達式

運行效果:

7. CAST表達式

用途:

強制類型轉換

語法:

@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
  as select from sbook
{
  key carrid,
  key connid,
      forcurkey,
      forcuram as origin_amount,
      case smoker
            when 'X' then cast ( forcuram as abap.fltp ) * 1.3
            else          cast ( forcuram as abap.fltp ) * 0.9
       end as final_amount

}

解釋:

  • CAST可以完成所需要的強制類型轉換,進而實現在CDS中的運算需求;其中abap.fltp代表轉換爲abap中的浮點型

運行效果:

8. COALESCE函數

用途:

常用用戶處理NULL的狀況,爲NULL語設定默認值

語法:

@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
  as select from sbook
{
  key carrid,
  key connid,
      coalesce( smoker,  'unassigned') as somker_status

}

解釋:

  • coalesce(arg1, arg2): 如果arg1不爲NULL則返回arg1的值,否則返回arg2的值

小結:

在本文中,我們介紹了SAP CDS View的最基本的運算語法,有關運算過程中遇到的問題,可以進一步根據ABAP 中的幫助文檔,查看相關的用法。

有關CDS的高階運算方式,將也會在後續的博客中陸續更新。

歡迎關注❤️、點贊👍、轉發📣!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章