Summary: I am writing this blog because we can’t directly compare fields from two different tables in CDS View. This document will help or guide you to achieve the compare field from two different tables. Will see step by step how can we accomplish the fields from two different tables using the “contain” condition.

Basically, we can write Contain comments for the same source field.

Like Below:

TABLE A
PRODUCT PRODUCT TEXT AMOUNT
P1 AA – USA 10
P1 UK – AB 30
P2 USA – AA 20
P2 USA – BB 40
P2 UK – AB 50

 

If we want to filter where “USA” word is used, then we will write CASE where Table-A- PRODUCT_TEXT like ‘%USA%’ then we will get the data only where we have “USA” word present. Example below

TABLE A
PRODUCT PRODUCT TEXT AMOUNT
P1 AA – USA 10
P2 USA – AA 20
P2 USA – BB 40

 

Now we have a different scenario. The case is mentioned below.

Now User will define the text in the different table to filter what data should be needed. Now we should not hard code the value directly. Based on the data maintained in table-b we need to filter the data in table-a.

We can’t directly compare fields with two different tables in CDS View. Will see step by step how can we achieve this.

Below are the Table A and B data.

                     TABLE A                                 TABLE B
PRODUCT PRODUCT TEXT AMOUNT PRODUCT PRODUCT TEXT
P1 AA – USA 10 P1 USA
P1 UK – AB 30 P2 UK
P2 USA – AA 20
P2 USA – BB 40
P2 UK – AB 50

 

 

 

Step1:

Create a New Table Function CDS View and write the below code:

@EndUserText.label: 'Text Filter Table Function'
define table function ZBI_CI_TEXT_TF

returns {

    mandt          : abap.clnt ; 
    PRODUCT        : abap.char(8);
    PRODUCTTEXT    : abap.char(15);
    
 }
implemented by method zcl_cds_text => get_text_match;

 

Step2:

Create a Class with zcl_cds_text name and write the below code:

 

CLASS zcl_cds_text DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_amdp_marker_hdb.


CLASS-METHODS get_text_match FOR TABLE FUNCTION ZBI_CI_TEXT_TF.
  PROTECTED SECTION.
  PRIVATE SECTION.

ENDCLASS.



CLASS zcl_cds_text IMPLEMENTATION.

  METHOD get_text_match BY DATABASE function FOR HDB
                        LANGUAGE SQLSCRIPT
                        OPTIONS READ-ONLY
                        using TABLEA TABLEB.  //Declare the Table here
    itab1 =
      SELECT T1.product,
             T1.Product_text,
             T2.product AS Product1,
             T2.product_text AS product_text1
        FROM TABLEA    AS T1
       INNER JOIN TABLEB AS T2
       ON T1.product_text LIKE '%' || T2.product_text || '%';


    RETURN SELECT   product,
                    Product_text,
                    Product1,
                    product_text1 FROM   :itab1;


  ENDMETHOD.
ENDCLASS.

 

Step3:

Create a New CDS View for Call Table Function and write the below code:

 

@AbapCatalog.sqlViewName: 'ZBICICTF'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'text Read from Table Function'
@VDM.viewType: #COMPOSITE
@Analytics.dataCategory: #CUBE

@ObjectModel.usageType:
{
    serviceQuality: 'D',
    sizeCategory: 'XXL',
    dataClass:'TRANSACTIONAL'
}
} 
define view ZBI_CI_TEXT_READ_TF 
as select from ZBI_CI_TEXT_TF 

{
//ZBI_CI_PAY_NOTE_TF
product
product_text
product1
product_text1

} where product_text1 <> ''  // here we will remove all blank record.

 

 

Now we will get data like below

PRODUCT PRODUCT TEXT AMOUNT
P1 AA – USA 10
P2 UK – AB 50

 

Conclusion:

Finally, we achieved. If the user changes the value in Table B it will automatically take the new value and filter out. From this, we compared field from two different tables in CDS View.

Hope the above information added a bit of value to your time and knowledge.

If you enjoyed reading please like and really appreciate sharing your valuable feedback or thoughts in the comment section.

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x