ABAP - Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Read Accesses →  SELECT →  SELECT - Examples → 

SELECT, CDS View with Input Parameters

This example demonstrates a read performed on a CDS view with pass by parameter.

Source Code

REPORT demo_cds_parameters.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA:
      from_distance TYPE s_distance VALUE 2000,
      to_distance   TYPE s_distance VALUE 6000,
      unit          TYPE s_distid   VALUE 'MI'.
    cl_demo_input=>new(
      )->add_field( CHANGING field = from_distance
      )->add_field( CHANGING field = to_distance
      )->add_field( CHANGING field = unit
      )->request( ).

    SELECT *
           FROM demo_cds_parameters( p_distance_l = @from_distance,
                                     p_distance_o = @to_distance,
                                     p_unit       = @unit )
           ORDER BY carrid, connid
           INTO TABLE @DATA(result).
    cl_demo_output=>display( result ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

SELECT is used to access a CDS view with parameters as a data source. Actual parameters are assigned to the input parameters of the view. The values of these actual parameters can be defined by input.

The view in question, demo_cds_parameters, has the following CDS source code with a list of input parameters:

@AbapCatalog.sqlViewName: 'DEMO_CDS_PARA'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_parameters
  with parameters p_distance_l:S_DISTANCE,
                  p_distance_o:S_DISTANCE,
                  p_unit:S_DISTID
  as select from spfli          
            { key carrid,
              key connid,
                  cityfrom,
                  cityto,
                  distance,
                  distid }
            where distid = :p_unit and
                           distance between :p_distance_l
                                        and :p_distance_o;

This means that those rows are read from the database table SPFLI whose distance in the passed unit is located between the two passed values.