SAP R/3 Document – ABAP/4 Query

ABAP/4 QUERY

A

BAP/4 Query is powerful tool to generate simple reports without the need for any coding. This is a very handy tool for the functional consultants who don’t have any prior programming experience. This document is not meant to teach ABAP/4 Query completely but is meant to give a lead to learn it. Summary : In order to create an ABAP/4 Query, one has to do the following: 1. Create an User Group 2. Create and Functional Area. In the functional Area, one has to mention how the data in the query is coming from (i.e. through a logical database or from an individual table using a program or joins etc.). Within the functional area you also have the functional groups categorizing fields from the tables into different groups. 3. Assign the User Group to the Functional Area 4. Create the Query based on the functional area created The above four steps have to be done to create an ABAP/4 Query. Creation of Query is a simple process. ABAP/4 Query provides for three different kinds of reports: Ø BASIC LIST Ø STATISTICS Ø RANKED LIST - Simple reports - Reports with statistical functions such Averages, Percentages… - for analytical reports

Within a query, you can define one basic list, up to 9 statistics and up to 9 ranked lists. These different list types may be combined in any permutation you like. You can also extend existing queries by adding more sub-lists. Unless you have specified otherwise, you see the basic list first, followed by the statistics and finally the ranked lists when you execute a query. One has to be clear as to what kind of report he / she wants and select the fields in the functional area accordingly and also the kind of report. One can also generate graphical reports in ABAP/4 Query. There are different paths to create the functional area, user group and queries. You can create any of these by choosing Tools à ABAP/4 Workbench à Utilities à ABAP/4 Query or executing the transaction codes for each of them: ü SQ01 for Query ü SQ02 for Functional Area ü SQ03 for User Group

Created / Compiled By : P. M. V. Subba Rao

1

M. c. Created / Compiled By : P. For creating a functional area with a logical database you have to mention the logical database name and then you can select the fields from the tables in the logical database for your query. Functional areas are assigned to user groups. The functional area could be created with a logical database or without a logical database. You can create a functional area with a single table You create a functional area using a ABAP/4 program You can create a functional area using Table Joins You can also use Sequential Dataset. b. Functional Area provides special views of logical databases. Subba Rao 2 . You create a functional area without a logical database with the following choices: a. First we will go through the procedure of creating the Function Area with a Logical Database. You can opt any one of them for creating a query. You can create a Functional Area without a logical database too. CREATING AN FUNCTIONAL AREA ( with a Logical Database) Ü The first step in creation of a ABAP/4 Query is to create an Functional Area. You can assign one functional area to many user groups.SAP R/3 Document – ABAP/4 Query The first step in the process of creating a ABAP/4 Query is to create an Functional Area. Part I deals with the steps involved in the creation of Functional Area. Ü To create an Functional Area goto Ü Tools à ABAP/4 Workbench à Utilities à ABAP/4 Query à Functional Areas Ü Give a Functional Area name and Click on Create button. d. In the functional area you specify how and where from the data in the query is going to retrieve from. V. They determine which fields of a logical database can be evaluated in queries. Functional Area can be created with or without a Logical Database.

g. 03 – Bookings etc.).SAP R/3 Document – ABAP/4 Query Ü A functional area is the definition of the fields in the query. Ü Tables of logical database / Joins show the tables from the logical database. V. Ü Save the Functional Area and Generate it. Ü Click on Functional Group button on the application toolbar to create the functional group for the fields from the tables in the logical database. To get the list of the fields from other tables select the table in the Tables from logical database / Joins box and double click. Ü You can add additional fields (user defined fields). 02 – Demo. Ü To change the list double click on the table name. Subba Rao 3 . M. Ü Window : Change Functional Area <Functional Area name> Ü In this screen in the Functional Groups box mention the functional Group number and description ( e. (e. Ü If the list of fields in the fields box is not completely displayed then to display the entire list of fields or to have broader display of place the cursor in the fields box area and click on Settings à Full Screen from the menu bar. Database : F1 Application : S. Ü Do the above step for the fields from each table. The fields from the tables are displayed in the Fields box. Created / Compiled By : P. This is the SAP defined flight database). Ü In the Fields box against the fields specify the functional group number in which you would like to include the field. Click on the SAVE icon and then on Generate icon on the toolbar to save and generate the functional area. These additional fields can be seen from the fields’ list of the assigned table. DON’T FORGET TO GENERATE THE FUNCTIONAL AREA. Ü In the next screen give a description to the functional area.g. additional tables and assign them to any table in the logical database. 01 – Flight. Ü In the Functional Area With Logical database box specify the logical database name. A functional area can be created with a logical database or without a using a logical database.

Choose GOTO à Code What code you want to write is up to you and the output of query you design. V. give the field name and the table name from the logical database in which this field has to include.SAP R/3 Document – ABAP/4 Query Ü To add additional fields from the menu bar choose GOTO à Additional Field In the next screen that you get. The point at which an ABAP/4 Query reads an additional table or performs calculations on additional fields depends on logical database table since the associated ABAP/4 code becomes part of the GET event processing of the table. Save the field details. Note : Please the online documentation for more information on this. In the next screen specify the Sequence. Subba Rao 4 . If you define Parameters as part of your selection screen you must also evaluate them in functional area code otherwise they will have no effect. length etc. M. Description. technical attributes for the fields like field type. and the second field uses the first field in its code then the sequence of the second field should be higher than the sequence of the first. Click on the enter icon. If you create two additional fields. You can also include tables. Created / Compiled By : P. Choose GOTO à Additional Tables You can also include your own selection criteria and parameters for executing the query other than default selection screen and parameters If you include your own selection criteria and parameters then you have to write a code for them to take any effect. You can to the same for any more fields. which you would like to include in the query. You can also write Code for the query that is to be executed at the time of executing the query. Heading. and then you can also specify any formula for the field.

Now go to PART II if you are creating a Query with Logical Database CREATING AN FUNCTIONAL AREA ( without a Logical Database) We will now see how to create a Functional Area without a Logical database. Ü Click on Back icon to come out. we have to keep in mind that the tables that are used have at least one field in common (i. M. The following steps show you how create the FA using table joins. If you want to display something at beginning of the page then you write the code for that in TOP-OF-PAGE. you write the code for that in START-OFSELECTION (Goto à Code à START-OF-SELECTION). the field name should be of the same name. If you want display something before or after the output of the query. We can create a functional area without a logical database using a program or a sequential set or table (Direct Read) or using table joins. Subba Rao 5 . domain / data element).SAP R/3 Document – ABAP/4 Query If you want to display something before the execution of the query / accessing the logical database. V. you write the code for that in END-OF-SELECTION.e. In order to create a FA using a table join. Next step is to create a User Group and assign it to the functional area. Ü Save and generate the functional Area. Created / Compiled By : P. To create an functional area without a logical database (using table join) do the following : Ü To create an Functional Area choose Tools à ABAP/4 Workbench à Utilities à ABAP/4 Query à Functional Areas Ü Give an Functional Area name and Click on Create button.

In the boxes against the fields. V. Now select two tables by selecting the check boxes for defining the join condition. Now proceed with PART II if you are creating a Query without a Logical Database Created / Compiled By : P. Ü After you press the enter key a check box will appear against the table names (left side). Ü In the Joined Tables area give the table names which you want to include in the join condition and press the Enter key. if there is a foreign key relationship defined between the tables then you will see 00 = 00 (signifying the join) displayed against the common fields of the tables else you can specify the join yourself by typing in 00 or 01 etc. Ü In the next screen give a description to the functional area. Ü You will get a message “Proposals Requested”. Click on Yes.SAP R/3 Document – ABAP/4 Query Ü A functional area is the definition of the fields in the query. Ü The base table name will be grayed out. Ü In the Functional Area Without a Logical database area give the base table name. three radio buttons will appear against the tables (right side) specifying the join type. Ü In the next screen that you get. Ü Check the table join check box and click on the table join button. Ü Click on Back arrow button to come back and continue defining the conditions for more tables if required. Click on the button (with + and down arrow symbol) to specify the join between the tables. M. Ü To define the join conditions choose EDIT à Join à Define condition. Ü You will see a mapping between the tables on the right hand side under the Join Conditions area. Subba Rao 6 . A functional area can be created with a logical database or without a using a logical database.

). Ü Tables of logical database / Joins show the tables from the logical database. additional tables and assign them to any table in the logical database. Ü You can add additional fields (user defined fields). give the field name and the table name from the logical database in which this field has to include. Ü Do the above step for the fields from each table. 03 – Bookings etc. Ü If the list of fields in the fields box is not completely displayed then to display the entire list of fields or to have broader display of place the cursor in the fields box area and click on Settings à Full Screen from the menu bar.SAP R/3 Document – ABAP/4 Query Ü Click on Functional Group button on the application toolbar to create the functional group for the fields from the tables in the logical database. Created / Compiled By : P. V. These additional fields can be seen from the fields’ list of the assigned table. Ü To add additional fields from the menu bar choose GOTO à Additional Field In the next screen that you get. Ü In the Fields box against the fields specify the functional group number in which you would like to include the field. Click on the SAVE icon and then on Generate icon on the toolbar to save and generate the functional area. Ü Window : Change Functional Area <Functional Area name> Ü In this screen in the Functional Groups box mention the functional Group number and description ( eg. The fields from the tables are displayed in the Fields box. M. 01 – Flight. Subba Rao 7 . Ü Save the Functional Area and Generate it. Ü To change the list double click on the table name. 02 – Demo. DON’T FORGET TO GENERATE THE FUNCTIONAL AREA. To get the list of the fields from other tables select the table in the Tables from logical database / Joins box and double click.

V. Note: Please the online documentation for more information on this. and the second field uses the first field in its code then the sequence of the second field should be higher than the sequence of the first. Ü You can also include tables. If you create two additional fields. Description. and then you can also specify any formula for the field. Ü You can also write Code for the query that is to be executed at the time of executing the query. If you define Parameters as part of your selection screen you must also evaluate them in functional area code otherwise they will have no effect. Heading. The point at which an ABAP/4 Query reads an additional table or performs calculations on additional fields depends on logical database table since the associated ABAP/4 code becomes part of the GET event processing of the table. You can do the same for any more fields.SAP R/3 Document – ABAP/4 Query Click on the enter icon. Save the field details. Subba Rao 8 . which you would like to include in the query. Created / Compiled By : P. technical attributes for the fields like field type. Choose GOTO à Code What code you want to write is up to you and the output of query you design. length etc. you write the code for that in START-OFSELECTION (Goto à Code à START-OF-SELECTION). M. Choose GOTO à Additional Tables Ü You can also include your own selection criteria and parameters for executing the query other than default selection screen and parameters If you include your own selection criteria and parameters then you have to write a code for them to take any effect. In the next screen specify the Sequence. If you want to display something before the execution of the query / accessing the logical database.

e. the following information is needed: à Short name à Field name Here. V. the local field inherits the currency or unit assignment of the reference field. use it as a reference field) The advantage of this option is that the local field can accept the same values as the reference field. Please note that headers should correspond to the field output length and that they may extend over two lines.SAP R/3 Document – ABAP/4 Query If you want display something before or after the output of the query. Ü When making this definition. Subba Rao 9 . choose Edit à Local field à Create on the Select Field screen. On all subsequent screens. without having to include an additional field. If you want to display something at beginning of the page then you write the code for that in TOP-OF-PAGE. à Header This is used when calculating column headers. Ü You can also define local fields for the functional area. In our example. Ü To define a local field. you can use any of the following options: – Refer to an existing field (i. the local field is identified via this name. Ü Save and generate the functional Area. à Functional group The local field is included in the functional group. Ü Click on Back icon to come out. the field Distance with the short name DISTANCE is a Created / Compiled By : P. If the reference field is a currency amount field or a quantity field. M. The definition of local fields allows you to generate new information from the fields in a functional area. à Attributes When defining the technical attributes of the local field. you write the code for that in END-OF-SELECTION. you enter a text which describes the field contents.

5) and character strings (e. n is the position of the first character and m is the position of the last character. – If an operand is a text field. You could do this if you wanted to emphasize particular values or lines in the list. the contents are interpreted as a symbol or icon and output is consequently in graphical format. date or time field. you can use the following notation to access the individual components of this field: date field [YEAR] year date field [ MONTH] month date field [DAY] day time field [ HOUR] hours time field [ MINUTE] minutes time field [ SECOND] seconds Created / Compiled By : P. a calculation formula consists of a single formula. Subba Rao 10 . 10 or 1. – Certain special fields are also available. Therefore. All formulae conform to the usual mathematical rules and contain operands and operators. These are: %NAME(the name of the user processing the query) %DATE(the current date when the query is being processed) %TIME(the current time when the query is being processed). If you are accessing the 2nd character to the 5th character of the field. you must specify the field size (number of characters/digits and number of decimal places).SAP R/3 Document – ABAP/4 Query quantity field. the local field SPEED is also a quantity field and has the same measurement unit as the field DISTANCE.Calculation formula You can determine the value of a field in either of the following ways: 1. – Define the field as a symbol or icon With these fields.g. since it contains a distance specification and a measurement unit. you can use the notation textfield n:m to access part of this field. Here. the notation would be text 2:5. . – If an operand is a date or time field. ‘ABC’). calculation.g. V. In the most simple case. M. With text and calculation fields. – Valid operands include the short names of fields as well as numeric constants (e. By using a calculation formula. – Define the field as a text.

Next step is to create a query. you specify the names of the users who are authorized to use this functional area / query. Next you create a user group. Ü Click on the Assign Functional Area button on the toolbar to assign the functional area to the user group. Created / Compiled By : P. V. Ü Save the User group and the come out by clicking the back icon. M. Subba Rao 11 . DIV is the operator for whole number division and MOD is the operator for the remainder in whole number division.SAP R/3 Document – ABAP/4 Query – Valid operators include the basic arithmetic operations plus DIV and MOD. You associate a user group with Functional Areas CREATING A USER GROUP Ü From the menu bar choose Environment à User Group In the next screen give a user group name . In the user group you specify the users who are authorised to use the queries under that user group. Ü Click on Create button Ü Give a description to the user group and specify the names of the users. – You can use parentheses in the usual way. In the user group.

You can display the data as a table.SAP R/3 Document – ABAP/4 Query CREATING A QUERY Ü To create a Query choose Environment à Queries Ü Give a Query name and Click on Create Button. Click on the check boxes to select the functional groups. Ü In the next screen give a description to the query. Select the fields that you want to include in your query report. V. you can do the following if required : • • You can specify Short names for the fields. specify the output length ( if required) and select the processing option from the further Processing Options box. M. Ü Select the functional area for the user group by double clicking on it or by clicking on the enter icon. Ü SELECT FIELD Screen. download it to a file. Ü Click on the Next Screen icon ( à ) on the toolbar. The list of Functional Groups that were defined in the functional area is displayed. Ü Click on the Next Screen icon ( à ) on the toolbar. display in a word processor etc. Choose Edit à Short Names à Switch On/ Off You can also change the selection text contents to the way you want it to appear in the selection screen. Here in this screen. which could include functional with logical database and without logical database. Subba Rao 12 . Please note that a user group can be assigned to any number of functional areas. Created / Compiled By : P. Ü SELECT FUNCTIONAL GROUP screen. The fields from this groups will only be displayed in the next screen. The fields from each functional groups is displayed. Select the ones required.

Ü SELECTIONS SCREEN . Select the field and then choose Edit à Column Header à Maintain Ü Click on the Next Screen icon ( à ) on the toolbar. For better looking output you can also check the Basic list with box and Columns separated by | check boxes on the top of the screen Ü Click on the Next Screen icon ( à ) on the toolbar. In this screen click on check boxes against the fields. If you click on the BlnkLn check box then there will be line after the output of fields against which the BlnkLn check box is checked Created / Compiled By : P. Click on Text check box if you want to have a Sub-heading for the selection for the fields Total for each field that is selected for sorting If you click on the Box check box then the output will be in box for the fields against which the box check box is checked. Ü Click on Basic List. You have to give a short name for the local fields that you create. Statistics or Ranked List) that you want to generate. Ü CONTROL LEVELS Screen. check the check box against the fields if you want to have the totals of the field in the output.SAP R/3 Document – ABAP/4 Query • You can also create local fields that you would like to include in this query only. Choose Edit à local field à Create You can include fields such as user name. In this screen you need to do the following : • • • • • Specify the lines on which the fields have to appear on the report layout Specify the order in which each field has to appear Specify the sort order ( if needed ) For the numeric fields in the list. M. you can do the following ( if required ) : • • • • • Specify the sort order as descending. • You can also maintain column headers for the fields. V. Ü Now you have to select the Query Report type (Basic List. Ü BASIC LIST LINE STRUCTURE Screen. system date. time etc. Subba Rao 13 . In this screen. Default is ascending.

Under Format you can specify the color of the column for each field. you can include or delete lines. Ü FIELD OUTPUT OPTION Screen. Change the label and click the Enter icon In the Page Header and Page Footer section. You can include the user name and date by specifying &N and &D respectively. Ü LIST LINE OUTPUT OPTIONS Screen. Middle Radio button – Unit appears after the figure and Last Radio button – Unit does not appear). Ü Save the Query now. Ü Click on the Next Screen icon ( à ) on the toolbar. Choose Edit à Line à Insert / Delete. Created / Compiled By : P. • • Ü Click on the Next Screen icon ( à ) on the toolbar. specify new position of display for the fields. This screen gives you the layout display of the selections that you have made for the query. Ü BASIC LIST HEADER. if you have any Quantity fields or currency fields then you can specify the position on display of the unit ( Left radio button – Unit appears before the figure. Here you can do the following if required : • • • change the output length of the field (s). Here you can • • • • Give a Page Header and Pager Footer to Query Change the field Labels. Place the cursor on the field and choose Edit à Column Header à Maintain. M. Ü Click on the Execute button twice to run the Query. V. If you want to have a label against the output of sort fields click on template check box and in the next screen that you get give the label name. Subba Rao 14 .SAP R/3 Document – ABAP/4 Query • Click on New Page to get the output for the fields against which New Page check box is checked on a new New Page Ü Click on the Next Screen icon ( à ) on the toolbar. In this screen you can specify the background color for the display of the output by selecting the color of the Header line in the output.

but ascending order is sometimes necessary.e. Conversion errors also have the same procedure as statistics As with statistic. Created / Compiled By : P. only a certain number of items are output. you can access the screens for maintaining the header and setting graphics parameters by following the normal sequence of query screens. You can then specify how many entries (i. To do this. The ranked list criterion is the sort criterion of the ranked list. Subba Rao 15 . you must define which fields are to be output and in what order. However. V. This would apply. you can specify ascending order for the ranked list criterion by selecting the column Asc. Subsequently. you choose the Next ranked list function. This makes ranked lists suitable for analyzing questions such as: "What are the 10 flight connections with the highest sales?" First. enter sequence numbers in the first column. In such cases. In our example. The proposed value here is always 10. The default sort sequence for the sort criterion is descending order. If you want to define another ranked list. currency fields and quantity fields require you to specify a reference currency or a reference unit which is used to convert all the amounts. You can also specify an output length and a rounding factor. This is necessary. output lines) you want the ranked list to have. since one query can contain several ranked lists. As with statistics. M. for example. THE PROCESS IS THE SAME EXCEPT THAT WHEN DEFINING THE QUERY YOU CHOOSE EITHER RANKED LIST OR STATISTICS LIST IN PLACE OF BASIC LIST. sorting is always by a numeric value known as the ranked list criterion. numeric values are also summed for key terms and displayed in a table. Here. you must assign a title to the ranked list. RANKED LISTS Ranked lists are special types of statistics. it is the field Free seats.SAP R/3 Document – ABAP/4 Query NOTE : YOU CAN DO THE ABOVE PROCESS FOR CREATING QUERIES WITH RANKED LISTS AND STATISTICS LIST. Additionally. to vendor sales figures which are stored as negative amounts. You select one of these fields as the ranked list criterion. The same rules apply for both these screens.

* <Query_head> This comment must always appear after your data declarations. ABAP Query uses the report as a model when generating the query report. The following is an example of the structure of the model report and the sequence of its components conform to a specific form : Report xxxxxxxx. The report must be free of syntax errors and have the same fixed point arithmetic setting as the functional area. the report has to be present.. This field string must contain the records to be evaluated. M.. On the Title and Database screen. If you use the component Maintain Queries to create a query for this functional area. which you can overwrite. DATA: BEGIN OF itab OCCURS xxx. using ABAP Query’s automatic data retrieval facility then. If necessary. structure tab to hold the records to be evaluated. PARAMETERS:. if such a table is used. The report that you would be using to retrieve the data should be created before hand because generating the functional area for the first time . V. Define Dictionary structure used to set up the functional area. again proceed as you would for creating a functional area. selection criteria. the model report itself thus remains unchanged.SAP R/3 Document – ABAP/4 Query Retrieving Data using a Program If you want to create a more sophisticated Query. SELECT-OPTIONS:.. which you cannot achieve.. DATA: .. define an INCLUDE STRUCTURE tab. Created / Compiled By : P. Define parameters. Subba Rao 16 . * Code to define the table itab. and fields. TABLES tab. internal table itab with the DATA: END OF itab. specify a structure in the field Table and select the field Data retrieval using program: The system will give you a report name by default..

LOOP.. Created / Compiled By : P. ENDLOOP . . *The data must now be in the field string tab. M. The two entries i. V. Subba Rao 17 . the user should be a member of at least one user group.e the two comment lines *<QUERY_HEAD> and *<QUERY_BODY> in the above example. Maintain (23) and Translate (67). DO.SAP R/3 Document – ABAP/4 Query Beginning of a loop to retrieve each record and place it in the field string tab SELECT. There are two means of assigning authorizations to the users : Ü User groups In order to create a query and maintain it.. AUTHORIZATIONS IN ABAP/4 QUERY : End users. this authorization object also contains a field ACTVT. but the system does not distinguish between upper and lower case. .. You can assign authorizations for this authorization object. * If necessary. Each user can work with the queries of the user groups to which he / she is assigned. Like every authorizations object. system administrators and translators must all have the appropriate authorizations to use ABAP Query. ENDDO. They start immediately after the character "<". Ü Authorizations A user can be given the authorizations to a query by using the authorization object S_QUERY. are fixed should be there in your ABAP report if you want to build a ABAP Query using and ABAP/4 program. code to format data * <Query_body> This comment must always be the last 'statement’ in the loop. which can accept the values Change (02).. Thus your access is restricted to specific functional areas (logical databases). • End of loop to retrieve each record ENDSELECT.

Subba Rao 18 . M. you see the names and titles of any queries already defined there in the lower part of the screen. you should terminate the processing and perform a comparison between the query and the functional area. proceed as follows: Choose Environment à Directories à Queries/user group. To do this. together with information about how the field definition has changed and where in the query the field is used. Ü To perform a comparison. in background. The default values of authorizations for the authorization object S_QUERY are: Ü S_QUERY_ALL : Change. To do this you would require variant. You then see a list of all the fields defined differently. V. or Place the cursor on the input field for the query name and display a list of possible entries by pressing F4 function key or by clicking the arrow on the field. Created / Compiled By : P. Ü You can display a list of queries for your current user group. you can carry out an automatic comparison for individual fields. Ü If you want to execute the query as a background job. Ü If differences occur between queries and functional areas. Maintain and Translate Ü S_QUERY_UPD : Change and Translate Ü When you have chosen a user group.SAP R/3 Document – ABAP/4 Query Depending on what the user values set in ACTVT field the user can manipulate / create the queries. select Query à Other functions à Compare. If you wish. choose Exec.

In the BASIC LIST LINE STURCTURE screen. double click on one of the lines on of the displayed list. What you need to do is create at least two functional groups with fields from different tables (E. Create the query with multiple line basic lists. Now execute the Query and after the first level of report. If data exists for the line selected then you should get the next level of display. to the fields from each table and the sequence of the fields on each line. A multiple line basic list is a list containing several lines with a different structure. While creating a Query.SAP R/3 Document – ABAP/4 Query Drill-down functionality in ABAP/4 Query You can also generate Drill-Down report using ABAP/4 Query. V. select the functional groups and the fields from these functional groups. the query (or query report) has to be entered in the interface as sender together with the recipient reports assigned to it Created / Compiled By : P. Subba Rao 19 . These tables should have a foreign key relationship with each other. M. You can expand or decompress the list by clicking on the Basic List button o the application toolbar. SFLIGHT and SBOOK). SPFLI. Also mark the Compressed Display check box on top of the screen.g. Before a further report can be called successfully. Calling Other Reports Reports residing in different applications can call each other in the R/3 System across the Report-Report-Interface RRI Reports are called using the interface either over the menu path Goto à Call report or by double-clicking (F2) on the appropriate line. assign different line nos.

Sign up to vote on this title
UsefulNot useful