Building an SAP Query with ABAP Code

作者:郑德鼎 约 6 分钟阅读 更新日期:2024-02-22 2 年前更新 标签:ABAP, 开发
目录

Add ABAP Code in SAPQuery

/SAP ABAP General /Add ABAP Code in SAPQuery

April 27, 2020

SAP ABAP General

You can add ABAP Code in SAP Query report to enhance your report, Open your infoset via tcode SQ02 and click Extras button toolbar.

You can choose any event depend on your requirement to add your ABAP Code.

These are the explanation for Event in listbox Code Section

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 SELECTION-SCREEN OUTPUT

This event is triggered before the display of selection screen.

START-OF-SELECTION

This event is triggered before starting any database accesses. Usually Data fetching logic goes under it.

RECORD Processing

Corresponds to the GET coding with Info-sets without a logical database. As there is no hierarchical view of the data, all the join tables are addressed.

END-OF-SELECTION :

The END-OF-SELECTION 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 aggregated calculations and actions that can only be done when data from all the selected records has been extracted.

For Example Case

Add Variable Declaration in DATA code section event.

DATA : gv_str TYPE string.

FIELD-SYMBOLS : <gi00> TYPE table,

<gw00> TYPE any.

<GI00> is the Internal table which contain result data that to be displayed on report. you can modify this internal table.

END-OF-SELECTION ( After List )

gv_str = '%G00[]'.

ASSIGN (gv_str) TO <gi00>.

LOOP AT <gi00> ASSIGNING <gw00>.

ASSIGN COMPONENT 'MSEG-BWART' OF STRUCTURE <gw00> TO <fv_any>.

IF <fv_any> IS NOT INITIAL.
SELECT SINGLE shkzg INTO lv_shkzg FROM t156

WHERE bwart = <fv_any>.

IF lv_shkzg = 'H'.

ASSIGN COMPONENT 'MSEG-DMBTR' OF STRUCTURE <gw00> TO <fv_any>.

<fv_any> = <fv_any> * -1.

ENDIF.
ENDIF.
ENDLOOP.

Building an SAP Query with ABAP Code

13853,350

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.

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 SELECTION-SCREEN OUTPUT

This event is triggered before the display of selection screen.

START-OF-SELECTION

This event is triggered before starting any database accesses. Usually Data fetching logic goes under it.

RECORD Processing

Corresponds to the GET coding with Info-sets without a logical database. As there is no hierarchical view of the data, all the join tables are addressed.

END-OF-SELECTION :

The END-OF-SELECTION 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 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 END-OF-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 B-date,

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_date-date.

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.

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.

field-symbols: <G00> type table.

field-symbols: <G00_WA> type any.

Enter the below ABAP code under END-OF-SELECTION(after list) Event

str = ‘%g00[]’.

ASSIGN (str) TO <G00>.

loop at <G00> assigning <G00_WA>.

…do whatever you like here….

endloop.

You can put your own logic inside the loop to perform. .

FollowLikeRSS Feed

Alert Moderator

Assigned Tags

ABAP Development

abap

coding

extra

infoset

query/report

sap

View more...

Similar Blog Posts

Modernization with the ABAP RESTful Application Programming Model (RAP)

By Carine Tchoutouo DjomoOct 18, 2021

Get Started with the ABAP Development Tools for SAP NetWeaver

By Olga DolinskajaJun 19, 2012

Spotlight on ABAP for SAP HANA – Again

By Carine Tchoutouo DjomoDec 21, 2017

Related Questions

SAP Community Coding Challenge - March 2020

By Thomas JungMar 17, 2020

HANA Ruleset query usage in ABAP Program

By Nallasivam BalagurusamyApr 28, 2022

Has The New SAP Community Killed The Community?

By Former MemberOct 27, 2016

13 Comments

Former Member

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 <G00> assigning <G00)WA>

Endloop.

Like 0

Reply

Alert Moderator

Share

Amit Diwane

Blog Post Author

May 10, 2016 at 9:53 am

You can write down the code under the END-OF-SELECTION(after list event) by Looping on the table G00.

Like 0

Reply

Alert Moderator

Share

Former Member

May 23, 2016 at 5:57 am

Hi Amit,

Thanks for your reply. I got the answer.

regards,

Sam...

Like 0

Reply

Alert Moderator

Share

Former Member

May 23, 2016 at 5:59 am

very useful document. I used this document for many times.. instead of keep remembering things, I remember this kind of document , so I can use it, whenever its required.

Like 0

Reply

Alert Moderator

Share

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 before is listed in the report result ?  Best Regards and thanks if you have time to save me

Like 0

Reply

Alert Moderator

Share

Amit Diwane

Blog 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.

Like 0

Reply

Alert Moderator

Share

AISWARIYA KUMAR N

March 20, 2020 at 5:35 pm

If I write the logic to limit o/p with a limit on selection screen input

loop <g00> to <gw>

Some operation

Endloop

If I write this in end of selection after list my selection screen layout got changed.

I need the layout as below

Like 0

Reply

Alert Moderator

Share

Francis Mullet

February 28, 2017 at 7:29 am

Is it possible to change, update and delete database table entries using ABAP coding in SAP Query?

Like 0

Reply

Alert Moderator

Share

Amit Diwane

Blog Post Author

March 13, 2017 at 10:26 am

Hello Francis,

You can make all the mentioned operations on the result set of the infoset queries which we received in G00 Internal table.

Like 0

Reply

Alert Moderator

Share

Cyrus Minab

June 13, 2017 at 1:39 pm

Hi Amit, In the Query there's a field with label SAP List Viewer where user can put the layout for the output. I created a parameter in the infoset called 'layout' and basically hidden SAP List Viewer now I want to transfer the value from my own parameter (i.e. layout) to the value of the SAP List Viewer which in the code is %ALVL...can this be done. Thank you

Like 0

Reply

Alert Moderator

Share

vijay dudhate

September 27, 2017 at 2:04 pm

Hello Amit,

How to transport this infoset from dev to Q with ABAP code in it

Like 0

Reply

Alert Moderator

Share

Amit Diwane

Blog Post Author

November 14, 2017 at 6:59 am

Hello Vijay,

You can check this link

https://archive.sap.com/discussions/thread/1574223

Like 0

Reply

Alert Moderator

Share

Rafael Lopez

April 2, 2020 at 1:53 pm

Hi,

Can I call METHOD set_table_for_first_display? I am trying but I have issues calling screen 100.

Thanks,

DATA : gv_str TYPE string.

DATA : lv_dmbtr TYPE dmbtr.

FIELD-SYMBOLS : <gi00> TYPE table.

FIELD-SYMBOLS : <gw00> TYPE any.

FIELD-SYMBOLS :  <fv_belnr> TYPE any.

FIELD-SYMBOLS :  <fv_gjahr> TYPE any.

FIELD-SYMBOLS :  <fv_bukrs> TYPE any.

FIELD-SYMBOLS :  <fv_drcr> TYPE any.

FIELD-SYMBOLS :  <fv_amt> TYPE any.

...

郑德鼎

关于作者:郑德鼎

企业信息化与 SAP 技术顾问,长期专注 SAP ABAP、FI/CO、MM、SD 等模块的技术分享与实战经验总结。查看更多介绍

来源说明:本文内容由「Building an SAP Query with ABAP Code.docx」整理生成,仅用于内部技术分享与学习交流。