Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Write dynamic where clause in ABAP Select query with CDS Views

0 Kudos

Dear Expert,

I want to achieve the below in the CDS view.

AS ABAP

ls_condtab-field = 'CARRID'.

ls_condtab-opera = 'EQ'.

ls_condtab-low = 'AZ'.

append ls_condtab into t_condtab.

clear : ls_condtab.

ls_condtab-field = 'CONNID'.

ls_condtab-opera = 'EQ'.

ls_condtab-low = '0017'.

append ls_condtab into t_condtab.

APPEND lx_condtab TO t_condtab.

CALL FUNCTION ‘RH_DYNAMIC_WHERE_BUILD’

EXPORTING

dbtable = ‘SFLIGHT’

TABLES

condtab = t_condtab

where_clause = t_where_clause

EXCEPTIONS

empty_condtab = 1

no_db_field = 2

unknown_db = 3

wrong_condition = 4

OTHERS = 5.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

****Dynamic Where Select Query

SELECT * FROM sflight INTO TABLE t_sflight WHERE (t_where_clause).

CDS VIew

@AbapCatalog.sqlViewName: 'ZDDL_TEST_DYN'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'Dynamic Where Condition CDS View'

define view ZCDS_TEST_DYNWHERE as select from sflight

{ sflight.mandt,

sflight.carrid,

sflight.connid,

sflight.fldate,

sflight.price,

sflight.currency,

sflight.planetype,

sflight.seatsmax,

sflight.seatsocc,

sflight.paymentsum,

sflight.seatsmax_b,

sflight.seatsocc_b,

sflight.seatsmax_f,

sflight.seatsocc_f }

Kindly help.

1 ACCEPTED SOLUTION

DoanManhQuynh
Active Contributor
0 Kudos

I guest you cant do it within CDS view but I think you could handle it using CDS table function (if you have ABAP 7.5 ). Read more here:

https://blogs.sap.com/2015/10/21/abap-news-for-release-750-cds-table-functions-implemented-by-amdp/

https://blogs.sap.com/2018/03/21/select-options-in-cds-using-table-function/

4 REPLIES 4

enric101
Active Contributor

0 Kudos

Hi,

Thanks for helping

DoanManhQuynh
Active Contributor
0 Kudos

I guest you cant do it within CDS view but I think you could handle it using CDS table function (if you have ABAP 7.5 ). Read more here:

https://blogs.sap.com/2015/10/21/abap-news-for-release-750-cds-table-functions-implemented-by-amdp/

https://blogs.sap.com/2018/03/21/select-options-in-cds-using-table-function/

0 Kudos

Hi,

Thanks for helping