Skip to content

ABAP Keyword Documentation →  ABAP - Dictionary →  Classic Objects in ABAP Dictionary →  Replacement Objects 

Replacement Object for Database Table

This example demonstrates a database table with a replacement object.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA(out) = cl_demo_output=>new( ).

    "Aggregate table (GTT)
    SELECT FROM demo_sumdist_agg
           FIELDS *
           ORDER BY PRIMARY KEY
           INTO TABLE @DATA(result_agg).
    DELETE FROM demo_sumdist_agg.

    "Table with replacement object
    SELECT FROM demo_sumdist
           FIELDS *
           ORDER BY PRIMARY KEY
           INTO TABLE @DATA(result).

    ASSERT result = result_agg.

    "Direct access to CDS view
    SELECT FROM demo_cds_sumdist
           FIELDS @sy-mandt AS mandt, demo_cds_sumdist~*
           ORDER BY PRIMARY KEY
           INTO TABLE @DATA(result_cds).

    ASSERT result_cds = result.

    out->write( result ).

    "Classic view on demo_sumdist without replacement object
    SELECT FROM demo_sumdist_obs
           FIELDS *
           ORDER BY PRIMARY KEY
           INTO TABLE @DATA(result_view_obs).
    IF result <> result_view_obs.
      out->write(
        'Classic view without replacement object differs.' ).
    ENDIF.

    "Classic view on demo_sumdist with replacement object
    SELECT FROM demo_sumdistview
           FIELDS *
           ORDER BY PRIMARY KEY
           INTO TABLE @DATA(result_view).
    IF result =  result_view.
      out->write(
        'Classic view with replacement object is the same.' ).
    ENDIF.

    out->display( ).

Description

This program accesses two database tables, DEMO_SUMDIST_AGG and DEMO_SUMDIST. These tables are identical except for the fact that the CDS view DEMO_CDS_SUMDIST is defined as a replacement object for DEMO_SUMDIST.

The database table DEMO_SUMDIST_AGG is filled with aggregated data (done here in the static constructor of the class demo). When DEMO_SUMDIST is accessed, the replacement object performs exactly the same aggregation:

@AbapCatalog.sqlViewName: 'DEMO_CDS_SUDI'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_sumdist(
client,
carrname,
distid,
sum_distance
)
as select from
scarr as s
join spfli as p on
s.carrid = p.carrid
{
key s.mandt,
key s.carrname,
key p.distid,
sum(p.distance)
}
group by
s.mandt,
s.carrname,
p.distid    

Similar access to the database tables produce similar results, which can be verified using the statement ASSERT.

A third SELECT statement accesses the CDS view DEMO_CDS_SUMDIST directly. To produce the same results set for the comparison with the other results as when accessing the database tables, the client column must be added, since the results set of a client-specific CDS view does not contain a column of this type.

A classic database view DEMO_SUMDIST_OBS contains the database table DEMO_SUMDIST as a basis table. There is no redirect to the replacement object of DEMO_SUMDIST in a SELECT. A classic database view DEMO_SUMDISTVIEW that is otherwise similar has the following CDS view as a replacement object:

@AbapCatalog.sqlViewName: 'DEMO_CDS_SUDIV'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_sumdistview
as select from
demo_cds_sumdist
{
key client,
key carrname,
key distid,
sum_distance
}    

This view accesses the replacement object of the database table DEMO_SUMDIST. When DEMO_SUMDISTVIEW is accessed using SELECT, its replacement object is evaluated and the result again matches the preceding result.