Skip to Content
Technical Articles

SAP Query using Standard Program Reports (SQ01 and SQ02)

This blog documents how you can use SAP Query (SQ01 and SQ02) to create infosets and queries using SAP standard program reports.

The following example shows how this can be done with standard report IW39  which uses program RIAUFK20 and structure RIHAUFK_LIST.

Enjoy..

PeteA

 

You can use standard SAP report programs in your SAP Queries. The following example shows how to can IW39 (RIAUFK20) with additional fields without the need for a developer key.

In the example below we will check if the equipment number on the order and notification are the same.


 

 

1. Create InfoSet via TCode SQ02:

This now gives the list of fields defined in structure RIHAUFK_LIST:

Click the  button to see the ABAP code which should look something like this:

 

report rsaqdvp_template.
*
*---------------------------------------------------------------------*
*   declarations
*   (insert your declarations in this section)
*---------------------------------------------------------------------*
data: rihaufk_list type rihaufk_list,
      it_data      type standard table of rihaufk_list.

field-symbols: <struc> type rihaufk_list.

tables: aufk.

data sel_tab type table of rihaufk_list with header line.

*-------------------------------------------------------------------*
*   selection screen statements
*-------------------------------------------------------------------*
*   (define your selection-screen here)
*select-options s_aufnr for aufk-aufnr.

* !! the following comment MUST NOT BE CHANGED !!
*<QUERY_HEAD>

*-------------------------------------------------------------------*
*   read data into IT_DATA
*-------------------------------------------------------------------*
*  (select your data here into internal table IT_DATA)
submit riaufk20 "with aufnr in s_aufnr
                with dy_selm  = 'D'
                via selection-screen and return.

import sel_tab from memory id 'RIAUFK20'.

free memory id 'RIAUFK20'.
it_data[] = sel_tab[].

*------------------------------------------------------------*
*   output of the data
*   (this section can be left unchanged)
*------------------------------------------------------------*
loop at it_data assigning <struc>.
  move-corresponding <struc> to rihaufk_list                  .
* !! the following comment MUST NOT BE CHANGED !!
*<QUERY_BODY>

 

In the above example we use the standard IW39 selection screen. However, you could define your own selection screen fields if required. This is done in the “selection screen statements” area. You would then need to pass these parameters to the RIAUFK20 program (and remove the “via selection-screen statement).

 

You can define additional fields, structures, etc for you report as follows:

 

 

Once the additional fields have been created you can see them in the Extras tab. You can then select the relevant field and click the  button to add ABAP code for the specific field:

Generate the infoset once you have finished.

 

2. Create SAP Query via TCode SQ01:

Assuming you want to enter your own selection–screen values, then create your Query as usual, but you do not need to specify any selection values as they are already defined in the InfoSet program code:

All other query functions are available e.g. local fields etc.

 

3. Test your SAP Query:

Execute the query and you should now see the results. The the example below we can see the two new fields we’ve added to the IW39 report (notification equipment and equipment mismatch flag):

3 Comments
You must be Logged on to comment or reply to a post.