In part two of this tutorial we’ll take a closer look at Infosets which form the data basis of SAP Queries
Creating an Infoset
The Next and most important step of SAP querying is to create this infoset. The infoset is the core definition of the query. It defines table joins and therefore the uniqueness of the data set below.
Taking a step back to general dataset joins, the more tables I join the more records and duplication of data I may end up with. In the example below I am interested in Sales order information
I might want to join three tables in the sales order structure, VBAK, VBAP, VBEP in the example below I end up repeating the records in VBAK and VBAP to allow for the multiple VBEP entries
Which is necessary if I need the schedule line information in VBEP, however if all the information I need is in VBAK and VBAP then I can simplify the report dramatically by leaving out VBEP. In this way I might generate two separate infosets, one with VBEP and one leaving it out. There are some advanced ways of simplifying the resulting structure which are discussed later on however the main thing to remember is design infosets specific for a use, and do not just add all the tables within a certain application area, as not only will the resulting report be confusing, it will also take considerably longer to process. So keep infosets simple and specific for their intended use.
Within the infoset transaction there are two main screens that you need to be familiar with;
· The graphical join view
· The infoset overview
As its name suggests the graphical join view is where you develop the join criteria between tables
The infoset view is processed after initial join criteria have been established. In this view you determine what fields from the joined tables can be used in queries, along with defining additional fields and tables.
To create an infoset, use transaction SQ02. First select the query area, add a name and press the create button
At this point you will get a dialog where you can enter a description for the Infoset. You should make this as descriptive as possible so that you can easily find the right infoset later on.
At this point you will also have to define the type of data source that will be used, these dictate the basis for the data, and the options are
· Table join
· Direct table read
· Logical database
· Data retrieval by program
What things can be queried
In the list above the most obvious is the table join, this as its name suggests means we define a table join criteria between multiple transparent tables such as joining EKKO and EKPO using a join criteria of common fields. This is probably where ninety percent of your SAP queries will be developed as we are generally looking to get a resulting dataset that is a join of multiple tables.
A direct table read may sound redundant as we can use SE16N to read a single table. However there may be reasons why we would want to create an infoset based on a single table. For example if you do not want to give a user access to SE16N then you can create an infoset based on a single table and even limit the fields visible. You can also augment a single table with additional fields in an SAP query. This can be used where your report is primarily based on the results of one table maybe with single fields from other tables. Instead of joining to those tables in their entirety, you can use some simple ABAP statements to bring in just the fields you want. This is a very powerful concept which we will discuss later
In addition to views which were discussed earlier, SAP also provides a concept of logical database where a group of related tables are joined using predefined program that can be used to read the whole dataset as if it were reading a single table. SAP uses logical databases widely throughout the various functional areas so you can often find a logical database that exists with all the data you need predefined.
In addition to basing infosets on tables you can also reference an ABAP program to retrieve data, this opens up any program that obtain data to being available to reporting, this can also include external programs, giving access to external data. It is beyond the scope of this document to look at this option in any detail.
Joining Tables
The main requirement for developing queries will be to join multiple tables together, based on what fields are used for the join and the type of join will determine what kind of information is returned by the query.
From within the initial screen in the infoset the table joins can be defined in a graphical manner. Additional tables can be added using the insert table button. ![]()
The insert table dialog appears
Enter the table name to join to and press the check mark
The system suggests the join criteria
However it is rarely correct, in this case it has correctly suggested joining EKKO-EBELN to EKPO-EBELN however it has also suggested joining EKKO-LPONR to EKPO-EBELP which is incorrect. It is often best to delete the suggested joins and start again based on the logic you want
This can be done by clicking on the joins then right clicking then selecting the delete link option
To make your own joins you can drag and drop from one table to another with the fields highlighted
You will notice a small red box highlighted as you hover over the various destination fields when you are over the appropriate field release the mouse to make the join
Inner Joins
By default all joins are inner joins. This means that the resulting report will only show a dataset where both tables have matching entries. In the case of our join of EKKO and EKPO this is fine as you would expect to have a line item for a purchase order. However this is not always the case. Say for example you are joining MARA and MARC to look at materials in plants; however you still want to see materials that are not extended to plants yet. In this instance you must use an outer join.
Outer joins
An outer join will show all the records that are in the first table along with corresponding records from the second table. It will show blanks if there are no values in the second table. If you wanted to create a report that shows materials not extended to plants then you could perform an outer join between MARA and MARC then look for null values in MARC fields. To make a join an outer join, right click on the join then select the left outer join.
Alias tables
In some cases you may want to join to the same table multiple times (to allow for different criteria). If you try and do this the system will error so you must first create an alias table, whereby you copy the table under a different name or alias first. For example if I wanted to create a Bill of materials report which would show planning parameters from the material master then I would link the material tables to the BOM header material, then I would need to Alias them so that I could link the same tables to the BOM components.
In the example above ZMARC is an alias of MARC
To create an alias table press the define alias table button ![]()
From the pop up press the create button then enter the table you want to alias and a unique alias name
You can now use the Alias as if it were an SAP table using the insert table button.
Field Groups
Once you have defined your table joins you can exit the join maintenance screen by selecting the Infoset maintenance button. You can always return to the join criteria and add / remove tables at a later time.
When you exit the join screen via the infoset maintenance button for the first time, you will be asked if you want to include all the fields from the corresponding tables, just key fields or none. SAP provides a way of grouping the fields you want to include in your query into logical groups called field groups.
I generally select all fields when I am first developing a query as I often find looking at the data helps define what is useful. Then when I have a fully functional set of queries I will simplify the infosets removing superfluous fields. If the query will end up with end users there may be fields you do not want to include. The more fields you include in the infoset will mean scrolling through them to select the right ones in a query.
In addition to the automatically generated field groups you can create your own and move fields into logical groupings
It is often useful to create your own field groups if you are going to add extras such as additional fields. To create a field group use the create icon
Enter a two digit key for the field group and a description
Once created the new field group shows up as empty in the field group area. Unassigned fields can be added by dragging and dropping.
The resultant change in field groupings can be seen on both sides.
Extras
In addition to creating table joins between tables. One of the most powerful (and dangerous) functions within the SAP query is the extras section. In this area it is possible to add
· Additional tables
· Additional fields
· Additional structures
· Selection criteria
· ABAP code
All with the use of freely definable ABAP code which may be entered without the need for a developer key.
Word of Warning
The ability to enter free form ABAP code in an SAP query means that this code can be entered into an infoset in any system where queries can be developed including directly into the production system. This combined with the fact that you do not need a developer key means that functional consultants may be tempted to do this themselves (thinking they have full understanding of what they are doing). Never (let me repeat that) NEVER run a query directly in a production client that has ABAP code in it without running the same exact query in other systems first. I know of one real world instance where a consultant ran a query on table VBFA (sales order document flow) and wanting to increase the performance he wanted to load an internal table ZVBFA with the data then reorganize it. Unfortunately due to a typo in the code he reorganized the VBFA table directly in production, bringing the company to a complete halt and requiring much emergency care directly from SAP. So as soon as you start adding extras to your query (no matter how innocuous). Be wise and learn from someone else’s mistake and never run your query untested in production.
That being said the area of extras really allows SAP query to be a powerful and useful tool.
Additional Table
Adding an additional table to an infoset may sound the same as adding it via a table join however you can add more complex selection logic via the additional table functionality. For example in a purchase order you can have multiple partner functions which are keyed by a different partner function such as VN, for vendor, OA for ordering address, PL for delivery plant etc. If I want to create a report just for ordering addresses I don’t want to see the other entries in the partner function table. I could add the table as a join and use my selection criteria to limit the results or I could add the table as an additional table and specify the ‘OA’ directly within the join criteria.
To create an additional table, select the extras button ![]()
This brings up the extras screen, showing multiple tabs and a list of any of the extras already defined.
Select the create icon then from the pop up select a table and the additional table radio button
From there you will get a join criteria pop up
In the same way that SAP tries to suggest table joins and doesn’t always get it right you need to validate the join criteria for yourself. In our example for ordering address we need to hard code PARVW to ‘OA’.
We now have the additional table assigned but with no fields allocated to field groups.
Additional Field
If you only need to bring in a single field from a table using specific logic then the additional field option allows for complete freeform logic to populate a custom field. From the extras tab select to add an additional field. You will need to give it a custom name
From the definition dialog you can either define the field directly as a certain type (such as character or date etc)
Or you can use the like command to reference an existing SAP field to copy. You must also set up a sequence for your additional items.
The sequence is important as if you want to use an additional field as the basis of another field as they process in order
Once the field is defined you need to enter the ABAP code to make it do something
You then get the very scary white space where you have full access to the ABAP language
“But I’m not a programmer” you may cry. Well even if you aren’t there are some simple code blocks you can use to do some quite powerful things. (Remember the word of warning though; don’t get in over your head)
Basically you will want to select some values from a table, maybe process them in some way and return a result. You may also want to use some ‘if then’ statements to make alter the data based on certain criteria.
Essentially the code has access to all the fields and data that are part of the table joins and the additional fields are executed at the lowest level of the report.
This means in the example below where I join VBAK, VBAP and VBEP, the additional field is executed for each record i.e. at the VBEP level (so you may get duplication of entries if it’s at a higher level)
This also means that the code may be executed hundreds of times and may slow the performance down.
The basic select statement is as follows.
This can be broken down as follows, my field I am trying to populate is called ZMATNR and its roughly based on MATNR.
The first thing I do is clear any preexisting value from ZMATNR
Then in line to I Select the field I want (MATNR) from the table I want that’s not currently in the query (MARC) and I copy it into my additional field (ZMATNR)
Then I specify the criteria for selecting that record form the large table MARC
I match plant with the plant value form the PO line item (EKPO-WERKS) and MRP controller with the MRP controller in the Po header (EKKO-DISPO)
Then I end my selection. Using multiple ands I can create complex criteria.
If my criteria returns multiple results I can sum them select the first , last etc I can also place hardcoded requirements and If then statements
Below is an example I have used in a custom IDOC report. It hard codes the segment I want to review to get the data I need.
clear ZE1BPEKKOA.
select sdata from edid4 into ZE1BPEKKOA
where DOCNUM = edidc-docnum
and SEGNAM =’E1BPEKKOA’.
endselect.
This returns a long field that has many different pieces of information. I know that the info I was looking for was 90 characters from the start and was 12 characters long hence I added the following logic
data l_str type char20.
l_str = ZE1BPEKKOA+90(12).
ZE1BPEKKOA = l_str.
The example below reads the schedule line table in a PO and sums all the schedule lines to give the total quantity to be delivered
clear zdelqty.
SELECT SUM( wemng ) FROM EKET into zdelqty
WHERE BANFN = eban-banfn
AND BNFPO = eban-bnfpo.
