ERPClimb logoERPClimb

Code examples · CDS views · Intermediate

CDS view entities: LEFT OUTER JOIN, CASE, CAST and UNION ALL

The problem

A report must list every order with the customer name, even for orders whose customer was deleted, and show a readable status.

How we solve it

  1. 1Tablescustomers, orders, archive
  2. 2Join viewLEFT OUTER JOIN
  3. 3Union viewUNION ALL
  4. 4Demo dataclass

The result

The join view shows two orders, one with an unknown customer, and the union view shows all three orders with their source.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A report must list every order with the customer name, even for orders whose customer was deleted, and show a readable status. A second list must show live and archived orders together and mark where each row comes from.

2. How we design it

Two tables hold the orders (live and archive) and one holds the customers. The first view joins orders to customers with a LEFT OUTER JOIN, so an order without a customer is not lost, and replaces the missing name and the status code with readable text using CASE and CAST. The second view stacks the two order tables with UNION ALL and adds a constant column that names the source.

Why we decided it this way

  1. 1LEFT OUTER JOIN from the ordersAn inner join would drop orders without a customer and the report total would be wrong.
  2. 2CAST for constant textsA text literal has the length of its own characters, so 'LIVE' and 'ARCHIVE' would have different types. CAST gives every branch of a CASE and every select of a UNION the same type.
  3. 3UNION ALL, not UNIONUNION removes duplicates and needs extra work. Live and archive rows are different rows by definition, so ALL is faster and correct.

Before you start

  • ADT and a package for the demo objects.

3. The code, step by step

Step 1 of 4: Create the three tables

Customers, live orders and archived orders.

CDS · Database tables · ZCUSTOMER, ZORDER, ZORDER_ARCH
// Customer table
@EndUserText.label : 'Customer'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zcustomer {
  key client      : abap.clnt not null;
  key customer_id : abap.numc(6) not null;
  name            : abap.char(40);
  country         : abap.char(2);
}
// Live order table
@EndUserText.label : 'Order'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zorder {
  key client      : abap.clnt not null;
  key order_id    : abap.numc(6) not null;
  customer_id     : abap.numc(6);
  @Semantics.amount.currencyCode : 'zorder.currency'
  amount          : abap.curr(15,2);
  currency        : abap.cuky;
  status          : abap.char(1);
}
// Archive order table
@EndUserText.label : 'Archived order'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zorder_arch {
  key client      : abap.clnt not null;
  key order_id    : abap.numc(6) not null;
  customer_id     : abap.numc(6);
  @Semantics.amount.currencyCode : 'zorder_arch.currency'
  amount          : abap.curr(15,2);
  currency        : abap.cuky;
  status          : abap.char(1);
}
Part by part (plain text)
  1. Customer table. A customer has a number, a name and a country. CLIENT is the first key field, as in every client-dependent table.
  2. Live order table. The order refers to a customer by number, but there is no hard link between the tables, which is why an order can exist without a matching customer. The amount belongs to the currency field.
  3. Archive order table. The archive has the same fields as the live table, which is what makes the UNION possible later.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

The join view shows two orders, one with an unknown customer, and the union view shows all three orders with their source.

What you should see

ZI_OrderWithCustomer 000001 Acme GmbH Open 000002 Unknown customer Closed ZI_AllOrders 000001 LIVE 000002 LIVE 000003 ARCHIVE

Try it and check the result

  • Activate the tables, the two views and the class. Run the class with F9, then open each view with F8.
  • ZI_OrderWithCustomer shows two rows. Order 000002 has the name Unknown customer.
  • Replace left outer join with inner join and activate: order 000002 disappears, which shows what the left join protects.
  • ZI_AllOrders shows three rows with the source LIVE or ARCHIVE.

Common mistakes

  • A condition on the right table in WHERE turns a left outer join into an inner join, because the NULL rows are filtered out. Put such conditions in the ON clause.
  • UNION needs the same number of columns, in the same order, with compatible types. A missing CAST on a constant is the usual cause of an activation error.
  • A view entity without a key cannot be used as a RAP business object. Add a key when one is needed.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: ABAP CDS view entities, S/4HANA 2020+ and ABAP Environment on BTP. Check on your release.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.