Lecture02 abap on line

1,371 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,371
On SlideShare
0
From Embeds
0
Number of Embeds
46
Actions
Shares
0
Downloads
232
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • 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.
  • Lecture02 abap on line

    1. 1. Lecture 2 Reading Database Tables BCO5647 Applications Programming Techniques (ABAP)
    2. 2. Readings & Objectives <ul><li>Readings </li></ul><ul><li>Keller & Kruger Chapter 8 Section 8.1.1 – 8.1.4 </li></ul><ul><li>Objectives This lecture will </li></ul><ul><li>Introduce the ABAP dictionary as part of SAP </li></ul><ul><li>Introduce ABAP Open SQL </li></ul><ul><li>Explore reading DB Tables using ABAP SELECT statements </li></ul><ul><li>Introduce System Variables and examine how they can be incorporated into an ABAP program </li></ul><ul><li>Examine program documentation options </li></ul>
    3. 3. The ABAP Dictionary <ul><li>The ABAP Dictionary administers the database tables. </li></ul><ul><li>The ABAP dictionary contains current information about a database table’s technical attributes. </li></ul><ul><li>When you look at a table in the ABAP Dictionary, you are looking at the DESCRIPTION of a table in the underlying database. </li></ul>
    4. 4. ABAP Open SQL <ul><li>In a report program you use ABAP Open SQL to read data from the database. </li></ul>
    5. 5. ABAP Open SQL <ul><li>Open SQL is SAP’s version of Standard SQL. </li></ul><ul><li>Open SQL statements are converted into database-specific native SQL statements. </li></ul><ul><li>Open SQL makes you code portable and fast. </li></ul>
    6. 6. Using SELECT to Retrieve Data 1 <ul><li>The SELECT clause describes whether the result of the selection will comprise several lines or a single line of data. </li></ul><ul><li>The FROM clause names the database from which data is to be selected. </li></ul><ul><li>The INTO clause determines the internal data objects into which the selected data is to be placed. </li></ul><ul><li>The WHERE clause defines the conditions that the selection results must fulfil. </li></ul>Reading by Single Record ACCESS
    7. 7. Using SELECT to Retrieve Data 2 <ul><li>Reading several rows using a loop. </li></ul>
    8. 8. Using SELECT to Retrieve Data 3 <ul><li>Reading several rows using an Array Fetch </li></ul>
    9. 9. Reusable Components for Data Retrieval <ul><li>If reusable components that encapsulate complex data retrieval are available, you should use them. There are four possible techniques: </li></ul><ul><ul><li>Calling methods of global classes; </li></ul></ul><ul><ul><li>Calling methods of business objects; </li></ul></ul><ul><ul><li>Calling function modules; </li></ul></ul><ul><ul><li>Including logical databases. </li></ul></ul>
    10. 10. Reading Data from the Database <ul><li>1. report y234802a. 2. tables: sbook. 3. select * from sbook. 4. write: / sbook-carrid, sbook-fldate. 5. endselect. </li></ul><ul><li>In the example above the tables statement: </li></ul><ul><ul><ul><li>allocates a table work area called sbook; </li></ul></ul></ul><ul><ul><ul><li>gives the program access to the database table: sbook. </li></ul></ul></ul><ul><li>The select statement begins a loop. The endselect marks the end of the loop. </li></ul><ul><li>Code between select and endselect are executed once for each row returned from the database . </li></ul>
    11. 11. Using WHERE to limit the Data returned with SELECT <ul><li>1. report y234802b. 2. tables sbook. 3. select * from sbook 4. where carrid = ’LH’. 5. write: / sbook-carrid, sbook-fldate. 6. endselect. </li></ul><ul><li>The where clause allows the program to specify which rows of data are to be returned by the select command. </li></ul><ul><li>1. report y234802c. 2. tables sbook. 3. select * from sbook 4. where carrid = ’LH’ 5. and connid = ’0400’. 6. write: / sbook-carrid, sbook-fldate. 7. endselect. </li></ul>
    12. 12. Using ORDER BY to sort the Data returned with SELECT <ul><li>1. report y234802d. 2. tables spfli. 3. select * from spfli 4. order by cityfrom. 5. write: / spfli-carrid, spfli-connid, 6. spfli-cityfrom. 7. endselect. </li></ul><ul><li>The order by clause returns the result of a select loop in ascending order by the nominated field. </li></ul>
    13. 13. Working with System Variables <ul><li>SAP’s R/3 system provides a number of system variables which are always available to your program. </li></ul><ul><li>All system variables have been defined in the Data Dictionary. </li></ul><ul><li>All system variables begin with the prefix sy- </li></ul><ul><li>For example: </li></ul><ul><ul><ul><li>sy-datum the current system date. </li></ul></ul></ul><ul><ul><ul><li>sy-uzeit the current time. </li></ul></ul></ul><ul><li>Two system variables useful when using the select statement are: </li></ul><ul><ul><ul><li>sy-subrc </li></ul></ul></ul><ul><ul><ul><li>sy-dbcnt </li></ul></ul></ul>
    14. 14. Working with System Variables <ul><li>sy-subrc can be used to test whether any rows from the database table were returned after using a select statement. </li></ul><ul><li>If rows were found, the value of sy–subrc is 0. </li></ul><ul><li>If no rows were found, the value of sy–subrc is 4. </li></ul><ul><li>1. report y234802e. </li></ul><ul><li>2. tables sbook. </li></ul><ul><li>3. select * from sbook </li></ul><ul><li>4. where carrid = ’LH’. </li></ul><ul><li>5. write: / sbook-carrid, sbook-fldate. </li></ul><ul><li>6. endselect. </li></ul><ul><li>7. if sy-subrc ne 0. </li></ul><ul><li>8. write / ’No records found’. </li></ul><ul><li>9. endif. </li></ul>
    15. 15. Working with System Variables <ul><li>sy-dbcnt can be used to determine the number of rows returned by a select statement. </li></ul><ul><li>sy-dbcnt can also be used as a loop counter. </li></ul><ul><li>1. report y234802f. </li></ul><ul><li>2. tables t001. </li></ul><ul><li>3. select * from t001. </li></ul><ul><li>4. write: / sy-dbcnt, t001-compyname. </li></ul><ul><li>5. endselect. </li></ul><ul><li>6. write: / sy-dbcnt, ’records found’. </li></ul>
    16. 16. Using the SELECT SINGLE statement <ul><li>The select single statement retrieves one row (record) from the database table. </li></ul><ul><li>select single does not start a loop and so there is no endselect statement. </li></ul><ul><li>A where clause must be included containing the value of a primary key that would uniquely identify one row in the database table. </li></ul><ul><li>1. report y234802g. </li></ul><ul><li>2. tables t001. </li></ul><ul><li>3. select single * from t001 </li></ul><ul><li>4. where compycode = ’A0125’. </li></ul><ul><li>5. if sy-subrc = 0. </li></ul><ul><li>6. write: / t001-compycode, t001-compyname. </li></ul><ul><li>7. else. </li></ul><ul><li>8. write: / ’record not found’. </li></ul><ul><li>9. endif. </li></ul>
    17. 17. Commenting Code and Formal Documentation <ul><li>An * (asterisk) in column one indicates that the entire line is a comment. </li></ul><ul><li>“ (double quotes) anywhere on a line indicates that the remainder of the line is a comment. </li></ul><ul><li>Formal documentation can be made in the documentation component of the program. </li></ul><ul><ul><ul><li>Go to the ABAP Editor : Initial Screen; </li></ul></ul></ul><ul><ul><ul><li>Click on the Documentation radio button; </li></ul></ul></ul><ul><ul><ul><li>Press Change; </li></ul></ul></ul><ul><ul><ul><li>Type in your documentation; </li></ul></ul></ul><ul><ul><ul><li>Press Save. </li></ul></ul></ul>
    18. 18. Commenting Code and Formal Documentation <ul><li>*----------------------------------------------* * Report Name: y234802h * Author: John Verbatum * Date: February 2007 *----------------------------------------------* * Description: * This program will ………… *----------------------------------------------* </li></ul><ul><li>report y234802h. </li></ul><ul><li>*----------------------------------------------* * Tables *----------------------------------------------* </li></ul><ul><li>tables: bsis, “Index for G/L Accounts </li></ul><ul><li>kna1, “Data in Customer Master </li></ul><ul><li>vbrp. “Billing: Item Data </li></ul><ul><li>*----------------------------------------------* * Variables *----------------------------------------------* </li></ul><ul><li>data: w_buzei(3) type n, “Line item number </li></ul><ul><li>w_monat(2) type n. “Fiscal Period </li></ul>

    ×