Skip to content

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.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    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( ).

    IF cl_abap_dbfeatures=>use_features(
      EXPORTING
        requested_features =
          VALUE #( ( cl_abap_dbfeatures=>views_with_parameters ) ) ).
      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)
             ##db_feature_mode[views_with_parameters].
      cl_demo_output=>display( result ).
    ELSE.
      cl_demo_output=>display(
        'Database system does not support views with parameters' ).
    ENDIF.

Description

The method USE_FEATURES of the class CL_ABAP_DBFEATURES defines whether the current database system supports with views with parameters. If this is the case, SELECT is used to access a CDS view of this type using the name of its CDS entity as a data source. Here, actual parameters are assigned to its input parameters whose value is determined using 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'
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.