Create Dynamic Query in SAP using Table Function

2.1k Views Asked by At

I am trying to create Dynamic SQL query using Table Function, so that it can be consumed in ABAP CDS views. For same I have developed an ABAP Class & defined a method "Get_data".

CLASS-METHODS get_data FOR TABLE FUNCTION ztablefunction.

I am expecting below results, I am getting table names from field . for each Condition record number it should go to its respective table and fetch few fields.

  • NOTE: Every table will have different structures only Condition
    Record no. will always be available, e.g. Table A900 may have Sales
    Org field, but A912 might not have Sales Org field

enter image description here

Below image depicts data in Dynamic Tables (Table Fields in Table A)

enter image description here

Below image depicts output I am expecting.

enter image description here

CLASS zcl_fiori_test DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
    
  PUBLIC SECTION.
    INTERFACES if_amdp_marker_hdb.
    
    CLASS-METHODS  get_data FOR TABLE FUNCTION ztablefunction.
    
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.
    
    
CLASS zcl_fiori_test IMPLEMENTATION.


  METHOD get_data BY DATABASE FUNCTION
                      FOR HDB
                      LANGUAGE SQLSCRIPT
                      OPTIONS READ-ONLY
                      USING zcds_test  dd03l.
    
    declare tabnamearr string;
    declare lv_knumharr string;
    declare iv_count integer;
    declare i integer;
    declare lv_query string;
    declare lv_kunnr string;
    declare lv_vkorg string;
    
    
    it_cdhdr = select :p_mandt as client,
        conditiontype as cond_type,
        '' as valid_start,
        '' as valid_end,
        changedocobjectclass,
        changedocobject,
        changedocument,
        changetransactioncode,
        cdhdrcreationdate,
        creationdate,
        keycombcondrec from zcds_test;
    
    select count( * ) into iv_count from :it_cdhdr;
    
    it_sel = select changedocobject, keycombcondrec from :it_cdhdr;
    
    it_final1 = select
        '' as mandt,
        '' as kappl,
        '    ' as kschl,
        '    ' as vkorg,
        '' as vtweg,
        '          ' as kunnr,
        '' as matnr,
        '' as kfrst,
        '' as datbi,
        '' as datab,
        '' as kbstat,
        '          ' as knumh
        from dummy; 
    
    it_kunnr = select '          ' as knumh, '          ' as kunnr  from dummy;

    it_vkorg = select  '          ' as knumh, '    ' as vkorg from dummy;'''
    
    for i in 1..iv_count do
    
        tabnamearr = :it_sel.keycombcondrec[i] ;
        lv_knumharr = :it_sel.changedocobject[i];
    
    
        IF exists(select *
              from    dd03l
              where  tabname = tabnamearr
                     and fieldname = 'KUNNR'
                      ) then
             lv_kunnr =  'select knumh, kunnr from ' || tabnamearr || ' where knumh = ' || lv_knumharr;
             EXECUTE immediate lv_kunnr INTO it_kunnr;
    
        END if;

        it_cdsview1 = SELECT a.changedocobjectclass,
        a.changedocobject,
        a.changedocument,
        a.changetransactioncode,
        a.cdhdrcreationdate,
        a.creationdate,
        a.keycombcondrec,
        cust.kunnr
         from zcds_test as a
        left join :it_kunnr as cust on a.changedocobject = cust.knumh;
    
    
        if exists(select *
              from    dd03l
              where  tabname = tabnamearr
                     and fieldname = 'VKORG'
                      ) then

            lv_vkorg =  ' it_vkorg = select knumh, vkorg from ' || tabnamearr || ' where knumh = ' || lv_knumharr;

            EXECUTE immediate lv_vkorg into it_vkorg;

        END if;
    
        it_cdsview2 = SELECT a.changedocobjectclass,
        a.changedocobject,
        a.changedocument,
        a.changetransactioncode,
        a.cdhdrcreationdate,
        a.creationdate,
        a.keycombcondrec,
        salesorg.vkorg from zcds_Test as a
    
        left join :it_vkorg as salesorg on a.changedocobject = salesorg.knumh;
    
    end for;
    
    return
        select <from Final Output table that should be matched with structure of Table Function>
    
  ENDMETHOD.
ENDCLASS.

Above code, I am not able to store data in temporary table as it does not support.

For alternative I have created structure/table using DUMMY table. But data is getting replaced with new data.

It might be a wrong approach, kindly suggest correct approach to achieve same. Please let me know, if you need more information

0

There are 0 best solutions below