The ABAP Dictionary The ABAP Dictionary is part of SAP. It contains definitions of tables found in the database (table name, field names, type, length etc.) Each table has a unique name within the SAP system. SAP comes with over 8000 table descriptions in the ABAP Dictionary. During implementation of an SAP system, specialist consultants use many of these tables to map to the organisation's existing database tables. The ABAP Dictionary sits on top of a database such as Oracle or Informix and acts like a remote control generating and sending SQL statements to it. For example, when you create a table definition in the ABAP Dictionary, SQL statements are generated and sent to the database, causing it to create the actual table in the database. When you want to modify the table, you must modify the table definition in the Data Dictionary - this generates more SQL causing the database to modify the table. When you look at a table in the ABAP Dictionary , you are looking at the DESCRIPTION of a table in the underlying DB.
ABAP Open SQL Reading data from the database is probably the most common activity in an ABAP program . A report program usually consists of a selection screen on which you define the dataset you wish to display . The user’s selection is integrated into an ABAP open SQL statement to extract the relevant data from the database. This data is then displayed as a “list” or “report”. Demonstrate zbmclec002a_demo What is OPEN SQL ?
ABAP Open SQL Open SQL statements are a version of Standard SQL. Open SQL goes beyond standard SQL by offering variants of the statements that only operate in ABAP programs and can simply or accelerate database access . Open SQL statements allow you to access the database in a uniform way from your programs, regardless of the database system being used. This is achieved because Open SQL statements are converted into database-specific (native) SQL statements by the database interface. For example, if you were running an Oracle database, your ABAP Open SQL would be converted by the database interface to Oracle SQL statements. Open SQL allows your ABAP programs to be portable between databases. For example, if your company wanted to switch from an Oracle DB to an Informix DB, it could change the database, and your ABAP code would continue to run without modification. Open SQL makes your code fast . SAP’s database interface buffers information from the database. When data is read from the database, it can be stored in buffers. If a request is made to access the same records, the request can be satisfied without having to go to the database. This buffering technique reduces the load on the database server and can speed up database access times by a factor of 10 to 100 times.
You use the Open SQL statement SELECT to program database read access. The SELECT statement contains a series of clauses, each of which has a different task: The SELECT clause describes whether the result of the selection will comprise several lines or a single line of data. The FROM clause names the database from which data is to be selected. The INTO clause determines the internal data objects into which the selected data is to be placed. The WHERE clause defines the conditions that the selection results must fulfil. Reading by Single Record ACCESS The SELECT SINGLE * statement allows you to read a single record from the database table. To ensure a unique access, all key fields must be filled in the WHERE clause. The * informs the database interface that all columns in that row of the database table should be read. If you only wish a specific selection of columns, you can list the required fields instead. In the INTO clause, enter the destination to where the database interface is to copy the data. The target area should have a structure identical to the columns of the database table being read.
Reading several rows using a loop If you do not use the SINGLE addition with the SELECT statement, the system reads multiple records from the database. In this example, the field list determines the columns whose data is to be read from the database. The number of requested rows to be read can be restricted using the WHERE clause. In the WHERE clause, you enter only the field names of the database table. The name of the database table that is accessed is found in the FROM clause. The database delivers the data to the database interface in packages. The ABAP runtime system copies the data records to the target area row by row using a loop. It also enables sequential processing of all the statements between SELECT and ENDSELECT.
Reading Several Rows Using an Array Fetch The INTO TABLE addition causes the ABAP runtime system to copy the contents of the database interface directly to the specified internal table. This is called an array fetch . Since the array fetch is not executed as a loop, you must not program an ENDSELECT statement.
Methods of global classes (covered later) Methods of business objects (NOT covered in this subject but Adv ABAP) Function modules (covered later) Logical databases (covered later)
Reading Data from the Database Line 1 - the report statement is required as the first line of a report program. Line 2 - the tables statement does 2 things: First, it allocates a memory area called the table work area. The name of this work area is called sbook . Second, it gives the program access to the database table called sbook . Note the program has 2 things named sbook : a work area and a table. Line 3 - Select is a looping command when it is used in conjunction with endselect . The select statement returns all rows contained in the named table, one at a time. The first row is returned and placed in the work area and made available for processing during the first pass of the loop. The second row is available during the second pass of the loop and so on, until all rows have been read from the table. The “*” means that ALL fields in each row of the database table are returned and stored in the table work area. Line 4 - the write statement is executed once for each row that is read from the database table into the table work area. Note that only two fields for each table row retrieved are printed. Also note that the field names are preceded by the table work area name and a hyphen.
Using WHERE to limit the Data returned with SELECT A where clause can be added to the select statement to restrict the number of rows returned from the database. In the first program, the only rows read from the table are those where the table field CARRID has the value LH. The and operator can be used to include more than one condition. The or operator can also be used in the where clause.
Using ORDER BY to sort the Data returned with SELECT Order By can have more than one field included Fields must be separated by spaces not commas, e.g. order by cityfrom cityto.
Working with System Variables SAP’s R/3 system makes system variables available within your program. You don’t have to define anything to access them, they are always available. They begin with the prefix sy- and are usually called sy (sigh) fields for short. All system variables are defined in the ABAP Dictionary.
Working with System Variables - sy-subrc To determine if the select statement returned any rows, you can test the system variable sy-subrc (sigh-sub-are-see) after the endselect statement If rows were found, the value of sy-subrc will be 0. If no rows were found, the value will be 4. If you code a select statement and want to test the value of sy-subrc , your test must come after the endselect statement. WHY? Answer: The code between the select and endselect is executed once for each row returned from the database. If no rows are returned from the database, the code between select and endselect is never executed. Therefore, you must code the test for sy-subrc AFTER the endselect . Demonstrate zbmclec002b_demo
Working with System Variables - sy-dbcnt The system variable sy-dbcnt keeps a count of the number of rows returned from a select statement. By testing the value of sy-dbcnt after the ENDSELECT statement, you can determine the number of rows returned. sy-dbcnt can also be used as a loop counter; between the SELECT and ENDSELECT it contains a count of the current loop. For the first row returned, sy-dbcnt will be 1, for the second it will be 2, and so on. After the ENDSELECT, it will retain its value and so will contain the number of rows selected. What output would result from the sample program? 1 Acme Australia 2 Acme North America 3 Acme Asia 4 Acme Europe 4 records found
Using the SELECT SINGLE statement You can use the SELECT SINGLE statement when you want a single row of data. For example, if you have a student number and you want to look up the student details. No ENDSELECT is needed because only one row is returned. If you know that only one row of data will always be returned, use this option as it is much faster more efficient than SELECT/ENDSELECT . To ensure that one unique row is returned, you should specify a primary field value in the WHERE clause.
Commenting Code and Formal Documentation All programs should be fully documented. There are 2 ways to put comments into your code: An * in column one indicates that the entire line is a comment. It will turn red within the editor to indicate that it is a full-line comment. “ anywhere on a line indicates that the remainder of the line is a comment. There is no end-comment character. That means, when you begin a comment, the remainder of the line will be a comment only. No more code can follow on that line. The comment ends at the end of the current line. Aside from comments, you can formally document your code in the documentation component of the program.