11/23/2016
Building an SAP Query with ABAP Code SAP Blogs
Get Started
Solutions
Partner
About
Community
/
Training
Community
Developer
Blogs
Building an SAP Query with ABAP Code November 20, 2015
| 548 Views |
Amit Diwane more by this author
ABAP Development abap | coding | extra | infoset | query/report | sap | sq01 | sq02
share 0
share 0
tweet
share 0
Follow
ABAP code is used with SAP query tool to enhance the query output. You can write down the code under the Extras tab for the Infoset in the SQ02 Tcode.
https://blogs.sap.com/2015/11/20/buildingansapquerywithabapcode/
1/6
11/23/2016
Building an SAP Query with ABAP Code SAP Blogs
You will find various coding events which are similar to classical ABAP report.
The code written in this code area is transmitted to the auto generated query report. You can write down your code in various coding section as per the use of them. DATA Section :
Global Data Declaration will go under this Event. INITIALIZATION Section :
As the name suggests it is used to initialize the variable with initial values. AT SELECTIONSCREEN OUTPUT :
This event is triggered before the display of selection screen. STARTOFSELECTION :
This event is triggered before starting any database accesses. Usually Data fetching logic goes under it. RECORD Processing :
Corresponds to the GET coding with Infosets without a logical
database. As there is no hierarchical view of the data, all the tables are addressed. ENDOFSELECTION :
The ENDOFSELECTION code consists of two parts ( Before list output and After list output ). The first part is processed before the list is output and the second part afterwards. It is the right section to do
https://blogs.sap.com/2015/11/20/buildingansapquerywithabapcode/
2/6
11/23/2016
Building an SAP Query with ABAP Code SAP Blogs
aggregated calculations and actions that can only be done when data from all the selected records has been extracted. However, there is a problem when queries use code in the ENDOF SELECTION section in combination with the Sap List Viewer format. In short, the code is never reached. Free Coding :
The code under this event is executed after all the events are triggered.
Logic to put extra field in the query output: Let you want a date field to be fetch from a ‘B’ table and has to be added to your query output. Define a Structure in DATA Event: data: begin of ty_date occurs 0, date like Bdate, end of ty_date. The logic to fetch the values for this field will go under the record processing event. In Record Processing Event write down the following code: CLEAR ty_date. CLEAR v_date. Select date from B into table ty_date. “You can mention your condition also with where clause Sort Table ty_date BY date DESCENDING. Read Table ty_date INDEX 1. v_date = ty_datedate. Generate the Infoset. Changes to the query: 1.Run SQ01 in another session. 2. Open your Query in change mode and select the field group in which has the added extra field. 3.Select extra field. 4 Click on basic list to select that field in your O/p list. https://blogs.sap.com/2015/11/20/buildingansapquerywithabapcode/
3/6
11/23/2016
Building an SAP Query with ABAP Code SAP Blogs
5.Click on Test button, and see if you are getting desired result.
Performing Operations on the final output table data: You’ll find table %G00 with final data to be displayed. Enter the following ABAP code under Data Section: data: str type string. fieldsymbols:
type table. fieldsymbols:
type any. Enter the below ABAP code under ENDOFSELECTION(after list) Event: str = ‘%g00[]’. ASSIGN (str) TO
. loop at
asg
. …do whatever you like here…. endloop. You can put your own logic inside the loop to perform. .
Alert
6 Comments You must be Logged on to comment or reply to a post.
Duraisamy Dhanaraj May 8, 2016 at 7:31 am
Hi Amit, I want to add the condition against the fields in the loop. How to do that ? Loop at
asg
https://blogs.sap.com/2015/11/20/buildingansapquerywithabapcode/
4/6
11/23/2016
Building an SAP Query with ABAP Code SAP Blogs
Endloop.
Amit Diwane Post author May 10, 2016 at 9:53 am
You can write down the code under the ENDOF SELECTION(after list event) by Looping on the table G00.
Duraisamy Dhanaraj May 23, 2016 at 5:57 am
Hi Amit, Thanks for your reply. I got the answer. regards, Sam…
Duraisamy Dhanaraj May 23, 2016 at 5:59 am
very useful document. I used this document for many times.. instead of keep ing things, I this kind of document , so I can use it, whenever its required.
Frederick Da Costa Ramos June 2, 2016 at 4:44 pm
Hi Amit, thanks for your explanation. I´m functional consultant and I´m used to create infoset/queries. I have an issue now that I need to summarize (collect) the result of the infoset I created. How can I do it ? How can I refer to the internal table the infoset creates during runtime and summarize it
https://blogs.sap.com/2015/11/20/buildingansapquerywithabapcode/
5/6
11/23/2016
Building an SAP Query with ABAP Code SAP Blogs
before is listed in the report result ? Best Regards and thanks if you have time to save me
Amit Diwane Post author June 3, 2016 at 5:12 am
Hello Frederick, The result set for the infoset queries is in G00 Internal table. You can consult with the abaper to make changes to the data in this final table. For runtime modification to the final output data of the query we generally write down the code. Please let me know if you need more information on same.
Share & Follow
Privacy
of Use
Legal Disclosure
https://blogs.sap.com/2015/11/20/buildingansapquerywithabapcode/
Copyright
Trademark
Sitemap
Newsletter
6/6