Monday, October 1, 2012

SAP Query

It’s time to extract data from our SAP System. How do it? It’s easy, with SAP Queries you have the possibility of creating your own extractions to manage the information you need.

To create a query you should follow 3 simple steps:

Step 1 – Create Infoset

The infoset is the set of tables where you will extract the data. Infoset is created in SAP with transaction SQ02 or following this path:

SAP Menu->Tools->ABAP Workbench->Utilities->SAP Query->SQ02 Infosets




When you are creating the infoset, data source is chosen as logical database or not (specifying directly the tables to extract).



If you choose the option of not using logical database, the selected table is shown in the screen and new ones could be inserted with the corresponding button ‘Insert table’.

Joins, link conditions, etc, also could be defined from this screen.


Once you have included all the required tables and their joins, is time to return to the previous screen with the back button. The system asks you about which field you want to be available for the later query creation.

Please note that if you want to use a particular table twice, SAP gives you the possibility using Alias. Click on ‘Alias’ button, specify the table (already used) and its alias, and then click on button ‘Insert Table’ and instead of specify a table, specify the alias created before.

It’s time to select the fields, save and generate the infoset. Now you have the Infoset ready to be used.

In SAP when you are creating queries, you have to take into account the Work Area. In SAP exists 2 type of Work areas, Standard Area (client-specific) and Global Area (cross-client).

Queries created in Standard Area won’t be transported to any client, however, queries created in Global Area could be transported, for example from development client to production one.

Note: A query created directly in production environment always must be created in Standard Area.

If you need to change the work area of a query, you could use SQ02 transaction, going to Environment->Query Areas and choosing the correct one.

Step 2 – Create User Group

The user group is set of authorized users for using the created query.

User Group is created with transaction SQ03, or following the path:

SAP Menu->Tools->ABAP Workbench->Utilities->SAP Query->SQ03 User Groups

In this menu, you create the user group with button ‘Create’ and Assign the users with the button ‘Assign users and Infoset’.


In the screen where you are assigning users to the user group, you can navigate to menu ‘Goto’ and ‘Assign Infoset’ (or visible button) and assign the Infoset to that user Group.

Step 3- Create Query

Is the query creation, based in the infoset (set of table) already defined.

The transaction to create the query is SQ01 or following the path:

SAP Menu->Tools->ABAP Workbench->Utilities->SAP Query->SQ01 Queries

The query is created for a specific user group, so the first step is choosing the proper user group, using the button ‘Other user group (Shift+F7)’.


Now is the moment to click on ‘Create’ button. The first thing the system is requesting is the name of the infoset where you want to select data.

In the following screen you specify the characteristics of your query, format, attributes, etc.

If you click on button ‘Next Screen (F6)’ you’ll see the list of fields available according to the selected infoset. You have to mark the fields you want to output in the query.

Clicking again on button ‘Next Screen (F6)’ you’ll see again the fields, in this case to select the fields you want to appear in the selection screen.

Also you can go to previous screen with the Menu ‘Goto’ and the proper submenu.

With Menu ‘Goto’->’Basic List’->’Structure’ you choose the extract layout of the query output, is the layout design (header, footer, etc…). If you have a look to the screen, button ‘Test (Ctrl+F8)’ and button ‘Check (Ctrl+F6)’ are available for your use.

At this moment you have created the query. It’s time to execute it. From SQ01, you can click on button ‘Execute (F9)’.

There is a second way to execute the query. When you create a query, an internal program is being created. That internal program also can be executed as a normal ABAP program, with SE38 transaction or with menus, jobs, etc.

But, how do you know the name of the program? It’s easy, from SQ01 select the button ‘Execute in Background (Shift+F6)’ and in the first screen you’ll see the name of the ABAP program generated with the query.

Query – QickViewer

There is another possibility of creating queries. Is with transaction SQVI or following the menu:

SAP Menu->Tools->ABAP Workbech->Utilities->SQVI QuickViewer


When this option you can create a basic query, selecting the tables where you want to extract the info and the corresponding layout. By this way is not necessary to create infoset as you saw in the explanation about SQ01.

When you click on button ‘Create’ the first screen is to choose data source. If you want a query to join tables, please select in Data Source ‘Table Join’.


A new window will appear in order to insert the tables you want and their relationship. Button ‘Inset Table (Shift+F1)’.

Clicking on ‘Back (F3)’ button, you navigate to the main screen where you have access to different tabs: Tab to select the list of output fields, tab to select the selection fields, tab to select the data source and tab to select the sort.

Please note that if you want to use a specific table twice, SAP gives you the possibility using Alias. Click on ‘Alias’ button, specify the table (already used) and its alias, and then click on button ‘Insert Table’ and instead of specify a table, specify the alias created before.

The way to execute is analog that the previous one. From SQVI clicking on ‘Execute’ button, or with the ABAP program generated (you can obtain the name clicking on button ‘Execute in background’) through SE38 (or whichever way you use an ABAP program, i.e. creating a job).

No comments:

Post a Comment