Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lecture02 abap on line


Published on

Published in: Technology
  • Be the first to comment

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>