Professional Documents
Culture Documents
Sap Query
Sap Query
Step-by-Step Tutorials
Topic
What is Query?
Purpose of Query.
2
3
4
Advantages of Query
Infosets/UserGroups/Query in Detail
Practical session covering important working models - I
Practical session covering important models-II
Various Lists and Background Scheduling
The SAP Query application is used to create reports not already contained in the default. It has been
designed for users with little or no knowledge of the SAP programming language ABAP.
SAP Query offers users a broad range of ways to define reports and create different types of reports
such as basic lists, statistics, and ranked lists.
These outputs can include lists on screens in table format, ALV grids, downloadable spreadsheets,
and downloadable flat files. The internal report generator creates an ABAP program corresponding to
the definition of the list.
Features:
The SAP Query comprises five components:
Queries
InfoSet Query
Infosets
User Groups
Translation/Query
Classic reporting- the creation of lists, statistics and ranked lists- are covered by the InfoSet Query
and Queries components. Other components range of functions cover the maintenance of Infosets,
the administration of user groups and also the translation of texts created in the SAP Query. All
data required by a user for a report can be read from various tables.
To define a report, you first have to enter individual texts, such as titles, and select the fields and
options, which determine the report layout. In the WYSIWYG (What You See Is What You Get)
mode, you can edit the lists using Drag & Drop and various toolbars.
Overview:
The following sections describes the individual SAP Query components and provides general
information about query areas, transport and authorizations
Menu Path
Used For
Maintaining Queries
Transaction
Code
SQ01
Maintaining Infosets
SQ02
Language Comparison
SQ03
SQ07
Query Components
The Queries component is used by end users to maintain queries.
You can carry out the following tasks:
Execute Queries and Generate Lists
Define Queries
Change Queries
Infosets Components
InfoSets are special views of data sources.
An InfoSet describes which fields of a data source can be reported on in queries.
InfoSets are assigned to user groups.
End-users are able to work only with those InfoSets that are relevant to their particular area, as
designated by the role or user group that they are assigned to.
Eg: Vendor master data can be important in purchasing as well as in accountancy. The relevant
InfoSet is assigned to both roles/user groups. This means that queries based on this InfoSet can
be copied and executed by both groups.
User Groups Components
1.
2.
3.
The User Groups component is used to maintain user groups. The system administrator uses it to set up the
work environment for end-users.
Every user assigned to the user group is able to execute the query.
Users are not allowed to modify queries from other user groups, although they may, under certain
circumstances, copy and execute
Translation/Query Component
A great deal of text is generated when defining queries, InfoSets, and user groups.
The SAP Query displays these texts in the language that you chose when you logged on to the
SAP system.
You can compare the text languages using the component Translation/Query.
A related text in one or more additional languages is made available for each of the texts
created when defining the query.
Query Areas
A query area contains a set of query objects (queries, InfoSets, and user groups) that are discrete and
consistent.
There are the following query areas:
Standard area
Global area
Standard Area
1.Client specific
2.Query objects are not attached to the Workbench Organizer
Advantage :-End users can develop queries (ad-hoc reports) in their own client that are not meant for
use in the rest of the system.
Global Area
1. Cross client
2. Query objects are attached to workbench organizer
Advantage:-The global query area is well suited for centrally developing queries meant for use and
distribution throughout the system.
Authorizations
End-users, system administrators, and translators must all be assigned the appropriate authorizations
allowing them to work with the SAP Query.
In order to give individual users targeted, specific rights, the following options are available:
Roles/user groups
Authorizations
Steps to create a Query
Step
Description
TCODE
SQ02
SQ03
SQ01
SAP Query
Step-by-step procedure in configuring SAP Query
SAP R/3 Query tools (SAP Query) are based on the following four main components:
Query Areas
Query Groups
InfoSets
Administrative Decisions (Company-specific)
Go to Transaction SQ03.
Ensure that you are in the correct SAP Query area by navigating to Environment Query Areas
Now in the main screen, enter the name of the Query Group in the User Group field and click on
CREATE
Enter
the
description
of
the
User
Group
in
the
next
popup
that
appears.
Click
on
the
Assign
Users
and
InfoSets
Enter the SAP User-Ids of all the users you wish to include in the test group.
button
Create InfoSets
Go to transaction SQ02
Ensure that you are in the correct SAP Query area by navigating to Environment Query Areas
Enter
the
name
of
the
InfoSet
you
wish
to
create
and
click
on
CREATE.
Enter
the
title
and
the
logical
database
on
the
popup
screen
We have used the LDB F1S (Flight bookings related) for our demo purpose.
that
appears.
Change
InfoSet
screen
appears.
Expand the tree on the left hand side to view the fields in each table. As seen on the screen, the left
side of the screen shows the tables and the fields. The right side displays the field groups.
Now we need to assign fields to the field groups. These field groups will display in the SAP Query
tool during reporting. Please note that only the fields that are included here will be available for
field selection in the SAP Query Tool that uses this infoSet as a data source.
Select the field group on the right side and then drag and drop the fields from the left side to this
field group.
When all the required fields are added, click on SAVE.
Go to transaction SQ02
Enter the InfoSet name created above and click on User Group Assignment button.
Select the query group ZSAPTECH_GRP from the above list and click on SAVE.
Basic List
- Simple reports
The first step in the process of creating an Ad-Hoc query is to create a Functional Area. Function area provides
special views of logical databases. They determine which fields of a logical database can be evaluated in
queries.
Go to Tcode SQ03.
Click on Choose
Create user group
Here you specify the users who should be authorized to run the query. A user group is always associated with a
Functional
Area.
- Go to the menu path Environment => User Group or SQ03 Give a user group name in the screen.
Specify the sap logins of the users whom you want to authorize for using the functional area and click on the
Create button.
Select User group as zdemo_user (Create Z user group)
Select assigned table check box with User group which u already created.(zdem_user)
Save it and back...
Go to tcode for SQ01..
Query zdemo_01 (Create zname)
Click on create button
Select output check box with right click select output-only value..
Select fields from which u already given info types
Click on continue....
Back...
Click on Execute button (F8)..
SQVI
Tools>Utilities>SQVI - QuickViewer
After you click on create button, system will populate the following window, complete the details with right
information
Once you have done with above steps you will get Choose Data Source screen. Here in the following step you
need to connect different tables properly in order to get task list data.
We are starting with MAPL (Assignment of Task Lists to Materials) table. Similarly, we need to take following
tables in order to get the appropriate information.
PLKO (Task list header)
PLPO (Task list - operation/activity)
PLAS (Task list - selection of operations/activities)
CRHD (Work Center Header)
CRTX (Text for the Work Center or Production Resource/Tool)
MARA (General Material Data)
MAKT (Material Descriptions)
Between PLPO and CRHD, the default link is with field OBJTY. You need to delete this link and create with
PLPO-ARBID and CRHD-OBJID. For this click on ARBID in PLPO table and drag the cursor towards OBJID in
CRHD table. Afterwards it should like
After you finish with above activities the screen will look like
Following tables explain the link details between different tables used above:
Table 1
MAPL
PLNTY
PLNNR
PLNAL
MATNR
PLKO
PLNTY
PLNNR
PLNAL
PLAS
PLNTY
PLNNR
PLNKN
PLPO
ARBID
CRHD
OBJID
Table2
PLKO
PLNTY
PLNNR
PLNAL
Table 3
MARA
MATNR
PLAS
PLNTY
PLNNR
PLNAL
PLPO
PLNTY
PLNNR
PLNKN
CRHD
OBJID
CRTX
OBJID
MARA
MATNR
MAKT
MATNR
In the following steps, mainly you need to select which fields you want to see in selection screen and which one
in output.
Similarly select all those fields from table enlisted which you want to see in Selection and Display screen.
For example, you want to see following fields in Selection Screen
o
o
o
Material
Plant
Task List Type
Material
Material Description
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
Plant
Group
Group Counter
Task List Description
Usage
Status
From Lot Size
UoM
To Lot Size
UoM
Operation/Activity
Work center
Work center description
Operation short Text
Operation Base Quantity
UoM
Standard value1
UoM for Std value1
Standard value2
UoM for Std value2
Standard value3
UoM for Std value3
Setup Group Key
Setup Group Category
Note: As per your requirement, you can select/deselect additional fields in Quick viewer: Initial screen
Its better to arrange Sort Sequence in order to get proper operation number in output for a combination of
Group, Group Counter, Material, and Plant.
ABAP Queries can be transported to different clients by the following two ways:
1.
2.
By using the transport option available on the application bar in Transactions SQ02 or SQ03.
On the next screen, all the options come up for transporting the queries. The transport action is performed
based on the radio button selected on the screen.
The transports are to be made in the specified order. The User Group is to be transported first, followed by the
Infoset and then the Queries.
By using the standard program RSAQR3TR:
Execute the standard program RSAQR3TR On the next screen, all the options come up for transporting the
queries. The transport action is performed based on the radio button selected on the screen.
Once the query is transported or uploaded in the destination system in the method specified above, the same
can be used there for displaying the data fetched from the database.
3. Put that custom function module in the infosets of all the queries. Keep all the executable codes of the
infosets in the ELSE part of the sy-subrc check.
Explanations:
Step 1: Create a Z-table ZQUERY_EXPIRE along with a table maintenance generator.
For more details on user group, infoset and query name, refer to table AQLQCAT.
Step 2: Maintain query names along with their expiry dates in the Z-table ZQUERY_EXPIRE via SM30.
Step 4: Call this custom function module from the START-OF-SELECTION code sections of all the infosets of
the queries that are maintained in the custom table ZQUERY_EXPIRE.
Here weve added the following code in all the infosets of the 3 queries maintained in the custom table.
CALL FUNCTION 'ZQUERY_EXP_CHECK'
EXCEPTIONS
query_expired = 1
OTHERS
= 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
WRITE: /5 'Hello1'.
ENDIF.
Test: Suppose todays date is 20/03/2009. The expiry date of the query BEJ_QRY_TEST1 was 18.03.2009 (as
maintained in the Z-table).
The expiry date of the query QRY_TEST3 is today (20/03/2009).
Scenario 1: Execute the query BEJ_QRY_TEST1 (expiry date is less than the current date) from SQ01.
Scenario 2: Execute the query QRY_TEST3 (expiry date is equal to the current date) from SQ01.
User Group).
b.
Add two selection criterion S_JOBNAM (Job Name) & S_STRTDT (Start Date) and one parameter
P_ENDTIM (Time) in the Selections tab.
Details:
Details: S_STRTDT.
Details: P_ENDTIM.
S_JOBNAM.
c.
DATA : FLAG(1),
FLAG1(1),
FLAG2(1),
status(15).
DATA: begin of gt_TBTCO occurs 0,
JOBNAME
type TBTCO-JOBNAME,
SDLSTRTDT type TBTCO-SDLSTRTDT,
SDLSTRTTM type TBTCO-SDLSTRTTM,
ENDTIME
type TBTCO-ENDTIME,
STATUS
type TBTCO-STATUS.
DATA: end of gt_TBTCO.
DATA: begin of gt_TBTCO1 occurs 0,
JOBNAME
type TBTCO-JOBNAME,
SDLSTRTDT type TBTCO-SDLSTRTDT,
SDLSTRTTM type TBTCO-SDLSTRTTM,
ENDTIME
type TBTCO-ENDTIME,
stat(15).
DATA: end of gt_TBTCO1.
DATA: begin of gt_TBTCO2 occurs 0,
JOBNAME
type TBTCO-JOBNAME,
SDLSTRTDT type TBTCO-SDLSTRTDT,
SDLSTRTTM type TBTCO-SDLSTRTTM,
ENDTIME type TBTCO-ENDTIME,
stat(15).
DATA: end of gt_TBTCO2.
DATA : gt_tbtco3 like gt_tbtco occurs 0 with header line.
DATA : gt_tbtco4 like gt_tbtco occurs 0 with header line.
d.
In the START-OF-SELECTION section, write the following code:
select JOBNAME
SDLSTRTDT
SDLSTRTTM
ENDTIME
STATUS
from TBTCO
into corresponding fields of table gt_tbtco3
where jobname in s_jobnam
and status = 'P'.
refresh gt_tbtco[].
clear
gt_tbtco[].
select JOBNAME
SDLSTRTDT
SDLSTRTTM
ENDTIME
STATUS
from TBTCO
into corresponding fields of table gt_tbtco
where jobname
in s_jobnam
and
SDLSTRTDT in s_strtdt.
check sy-subrc = 0.
sort gt_tbtco by JOBNAME SDLSTRTTM.
gt_tbtco4[] = gt_tbtco[].
delete gt_tbtco where ENDTIME < P_ENDTIM
and ( status = 'F' or status = 'A' ).
sort gt_tbtco by JOBNAME SDLSTRTTM.
Loop at gt_tbtco.
at new jobname.
flag = 'X'.
endat.
if flag = 'X'.
MOVE-CORRESPONDING gt_tbtco to gt_tbtco1.
if gt_tbtco-status = 'F' .
if ( gt_tbtco-SDLSTRTTM le P_ENDTIM ) .
gt_tbtco1-stat = 'Active'.
append gt_tbtco1.
clear gt_tbtco1.
else.
gt_tbtco1-stat = 'Released'.
append gt_tbtco1.
clear gt_tbtco1.
endif.
elseif gt_tbtco-status = 'F' and gt_tbtco-SDLSTRTTM ge P_ENDTIM.
gt_tbtco1-stat = 'Cancelled'.
append gt_tbtco1.
clear gt_tbtco1.
else.
status = gt_tbtco-status.
case status.
when 'R'.
if ( gt_tbtco-SDLSTRTTM le P_ENDTIM ) .
gt_tbtco1-stat = 'Active'.
else.
gt_tbtco1-stat = 'Released'.
endif.
when 'Z'.
gt_tbtco1-stat = 'Suspended'.
when 'A'.
gt_tbtco1-stat = 'Cancelled'.
when 'P'.
gt_tbtco1-stat = 'Scheduled'.
when 'S'.
gt_tbtco1-stat = 'Released'.
when 'Y'.
gt_tbtco1-stat = 'Released'.
endcase.
append gt_tbtco1.
clear gt_tbtco1.
endif.
endif.
CLEAR FLAG.
clear status.
endloop.
sort gt_tbtco1 by jobname.
sort gt_tbtco3 by jobname.
Loop at s_jobnam.
read table gt_tbtco1 with key jobname = s_jobnam-low.
if sy-subrc = 0.
flag1 = 'X'.
move-corresponding gt_tbtco1 to gt_tbtco2.
append gt_tbtco2.
clear gt_tbtco2.
else.
read table gt_tbtco3 with key jobname = s_jobnam-low.
if sy-subrc = 0.
flag2 = 'X'.
move-corresponding gt_tbtco3 to gt_tbtco2.
gt_tbtco2-stat = 'Scheduled'.
append gt_tbtco2.
clear gt_tbtco2.
else.
read table gt_tbtco4 with key jobname = s_jobnam-low.
if sy-subrc = 0.
flag2 = 'X'.
move-corresponding gt_tbtco4 to gt_tbtco2.
if gt_tbtco4-status = 'F'.
gt_tbtco2-stat = 'Finished'.
else.
gt_tbtco2-stat = 'Canceled'.
endif.
append gt_tbtco2.
clear gt_tbtco2.
endif.
endif.
endif.
if flag1 <> 'X' and flag2 <> 'X'.
gt_tbtco2-jobname = s_jobnam-low.
gt_tbtco2-stat = 'Not Scheduled'.
append gt_tbtco2.
clear gt_tbtco2.
endif.
clear : flag1,flag2.
endloop.
if not gt_tbtco2[] is Initial.
* Display List of Cases to be Cancelled
CALL FUNCTION 'RS_COMPLEX_OBJECT_EDIT'
EXPORTING
object_name
= 'RESULTS'
changing
object
= GT_TBTCO2[]
EXCEPTIONS
OBJECT_NOT_SUPPORTED
= 1
OTHERS
= 2.
endif.
EXIT.
Generate the infoset and then attach it to the user group ZPROD_BEJ.
Step 3: Create the query.
Using t-code SQ01, create one query BATCH_JOBLIST for the infoset BATCH_JOBLIST_REPORT.
Step 4: Execute the query from SQ01 with proper variant for the list of background jobs whose hourly status
needs to be found out.
Select the variant. Suppose we want to find out the background status at 2 PM for the below list of jobs:
PROC_CHGPNTRS_ENERGYCARE
PROCESS_CP_ACTIVITY
PROCESS_CP_CONTACT
PROCESS_CP_CONTRACT
PROCESS_CP_SALESORDER
PROCESS_IDOC_IN_ACTIVITY
PROCESS_IDOC_IN_ADDRSYNCH
PROCESS_IDOC_IN_BPCAMERGE
PROCESS_IDOC_IN_OAMUPDATE
PROCESS_IDOC_IN_PARTNRUPD
PROCESS_IDOC_IN_PREPAY
PROCESS_IDOC_IN_SALESORDR
PROCESS_IDOC_OUT_ACTIVITIES
PROCESS_IDOC_OUT_AQSND0055
PROCESS_IDOC_OUT_CONTACTS
PROCESS_IDOC_OUT_CONTRACT
PROCESS_IDOC_OUT_OBJSD0064
PROCESS_IDOC_OUT_SOSTATUS
PROCESS_CP_FILES
PROCESS_IDOC_OUT_FILES
SWWERRE
SWEQSRV
SWWDHEX
ZUBC_DELETE_WORKITEMS
ZZ_IDOC_DISPATCH
Z_APP_LOG_DEL_DAILY
ZUBC_DELETE_IDOCS
EMMA_AUTOPROC_SB61
ZUUBI_REPLACEMENT_BILLING_01
ZUBC_DELETE_APP_LOGS
SAP_ARCH_RETHIST
SAP_ARCH_PAYLOTS
SAP_ARCH_RETLOTS
SAP_APURG_MDR1
Put all these job names in the select option for job name. In the variant, the date should be equal to current date
and time should be current time and should be done as follows:
Now selecting the variant JOBLIST (JOB LIST STATUS), execute the query.
This is the status update of all the listed background jobs for the hour 14 i.e. 2 PM.
1.
2.
3.
4.
Here in this document, well demonstrate building SAP query using 2 nd method (Direct read of table).
1. Create InfoSet by using t-code SQ02:
Press enter.
Press enter.
3. Press Code.
And write the following code in START-OF-SELECTION section to display a few field data in ALV grid
display report format.
* Retrieve data from tables SFLIGHT and SCARR
SELECT m1~carrid
m1~connid
m1~fldate
m1~seatsmax
m1~seatsocc
m2~carrname
m2~currcode
INTO TABLE i_flight
FROM sflight AS m1 INNER JOIN scarr AS m2
ON m1~carrid = m2~carrid.
CLEAR: wa_flight.
* Populate the field catalogs
wa_fldcat-col_pos = 1.
wa_fldcat-fieldname = 'CARRID'.
wa_fldcat-seltext_l = 'Airline carrier ID'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 2.
wa_fldcat-fieldname = 'CONNID'.
wa_fldcat-seltext_l = 'Flight connection Id'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 3.
wa_fldcat-fieldname = 'FLDATE'.
wa_fldcat-seltext_l = 'Flight date'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 4.
wa_fldcat-fieldname = 'SEATSMAX'.
wa_fldcat-seltext_l = 'Maximum capacity'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 5.
wa_fldcat-fieldname = 'SEATSOCC'.
wa_fldcat-seltext_l = 'Occupied seats'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 6.
wa_fldcat-fieldname = 'CARRNAME'.
wa_fldcat-seltext_l = 'Airline name'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
wa_fldcat-col_pos = 7.
wa_fldcat-fieldname = 'CURRCODE'.
wa_fldcat-seltext_l = 'Local currency of airline'.
APPEND wa_fldcat TO i_fldcat.
CLEAR: wa_fldcat.
* ALV grid display report
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat
= i_fldcat
TABLES
t_outtab
= i_flight
EXCEPTIONS
program_error = 1
OTHERS
= 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
EXIT.
6.
7.
8.
Now assign the user group ZUGRP_BEJ to the InfoSet ZINFOSET_BEJ. Ive assigned two users in my
user group.
Save it.
9.
The following selection screen will appear where we need to put the flight date.
Enter the flight date as 11.06.2008 and execute the query; well get the following ALV grid display report.
SAP Query
Step-by-step procedure in creating a simple list using SAP Query
1. Go to Transaction SQ01.
2. The title bar shows you the query group you are currently in.
3. Ensure that you are in the correct SAP Query area by navigating to Environment Query Areas
4.
In the field Query, enter the name of the query you want to create and press CREATE.
Enter
the
title
and
any
other
notes
that
are
required.
Click on Next
screen button as shown in the above screenshot.
8. All the field groups available with the InfoSet are listed above. We can select the field groups that
need
to
be
listed
in
the
report.
13. Click on the Basic List button to create a SAP Basic list query.
14. For each field, specify the line number and sequence number as you would like to appear on the
report. You can also use this screen to indicate sort order, totals and counts (as necessary).
As shown above, the line structure would display the appearance of fields on the selection-screen.
15. Click on SAVE.
16. Click on EXECUTE button to execute the report. As it is seen on normal reports, here also the
selection-screen appears.
\*
INCLUDEPICTURE
"http://www.saptechnical.com/Tutorials/SAPQuery/Part2/Page1.16.gif"
MERGEFORMATINET
Infosets Components
InfoSets are special views of data sources.
\*
Do you need to be able to change the long texts and headers for the selected fields?
Assign Data sources
1.
2.
3.
Name
Authorization group: This means that only users authorized to execute programs from this authorization
group are able to execute these queries. Use Tcode: AUTH_DISPLAY_OBJECTS
Choose Data Source:
Table join using a table: Is used when Query is prepared for retrieving data from more than one
database tables having relationships.
Reading tables directly: Is used when Query is prepared for retrieving data from one table.
Logical databases: Is used when Query is prepared for retrieving data based on a
Logical database (LDB)
4.
Using programs to retrieve data: Here a program can be written with own logic. Declaration in data section of
the program will help the query to select the elements of data to be retrieved from database. As per the logic,
data is validated, processed. The selection screen can be prepared in Query and data is retrieved for output.
Sequential datasets
4.
Options
Users are not allowed to modify queries from other user groups, although they may, under
certain circumstances, copy and execute
Tcode-SQ02
3. To create a query
Tcode-SQ01