Saturday, 22 July 2017

Simple Program to view tables in single view

A lot of times we need to view data from different tables in a single view to perform analysis.

It could be analysis for development or production. In those scenerios we can create quick SAP Query – but problem with Query is that if two tables are not joined you cannot view data from two more more independent tables.

In those cases we can use the below report which will fetch data from different tables and present it in single ALV or Browser page. The report logic is as given below.

Selection screen


The selection screen would consist of 5-6 different set of fields for table name and the where clause.

If you want data from maximum 5 tables you can have 10 fields – one for table name and one for where clause as given below.

You can add buttons to Copy Where to all, Delete All where clauses etc.

You can also create Join checkbox to join two tables and fetch common data.

ABAP Testing and Analysis, ABAP Development

Dynamic Itab creation


Then using RTTC you can create the internal table dynamically for the input table name on selection screen as given below.

      lo_struct ?= cl_abap_typedescr=>describe_by_name( p_tab ).

      lt_comp  = lo_struct->get_components( ).

      APPEND LINES OF lt_comp TO lt_tot_comp.


      lo_new_type = cl_abap_structdescr=>create( lt_tot_comp ).

      lo_new_tab = cl_abap_tabledescr=>create(

                  p_line_type  = lo_new_type

                  p_table_kind = cl_abap_tabledescr=>tablekind_std

                  p_unique     = abap_false ).

      CREATE DATA w_tref TYPE HANDLE lo_new_tab.

      ASSIGN w_tref->* TO <dyn_tab1>.


You need to declare field symbol <dyn_tab1> as many times as the number of tables allowed on selection screen.

Fetching data from tables


You can fetch data from tables from selection screen using below format giving dynamic table name and where clause.

  SELECT * FROM (p_tab) UP TO 100 rows

    INTO TABLE p_itab

  WHERE (p_where) .

In case of Join the Select query will look like this – you need to do that based on Join checkbox.

SELECT * FROM (p_tab) UP TO 100 rows
 INTO TABLE p_itab2
FOR ALL ENTRIES IN p_itab1
 WHERE (p_where).

Calling browser functionality


You can use the cl_abap_browser class to write the fetched data into it. Declaration is given below.

     oref TYPE REF TO cl_demo_output_stream,

     output_stream TYPE xstring.

To add data to it use below.

   oref->write_data( p_tab ).

   oref->write_data( p_table ).


After all the data is fetched you can use below statement to display browser window with the tables all in single page.

    output_stream = oref->close( ).

You can use REUSE_ALV_BLOCK_LIST_APPEND, REUSE_ALV_FIELDCATALOG_MERGE and REUSE_ALV_BLOCK_LIST_DISPLAY to display it in standard ALV format.

Example output in ALV format


ABAP Testing and Analysis, ABAP Development

Example Browser format


ABAP Testing and Analysis, ABAP Development

Uses
  • It is useful to see multiple table contents in single window so you can analyze data.
  • For eg. to see all the information about a message type you can check tables – EDP13, EDP21 and EDP12 in a single window to get process code, FM etc.
  • You can create variants with multiple tables to see various information.
  • The program can be expanded to use Joins and get data from multiple tables.

Caution

The program should only be used in Development or Quality environment as there can be malicious code injected using SQL. Or the program should be modified to check the WHERE clause for malicious code before executing.

1 comment:

  1. This blog was written by me on SAP Blog, please take this down or else I will report to Blogger as this violates its terms and conditions.

    ReplyDelete