Reading Database Tables: BCO5647 Applications Programming Techniques (ABAP)

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Lecture 2 Reading Database Tables

BCO5647 Applications Programming Techniques (ABAP)

Readings & Objectives


Readings Keller & Kruger Chapter 8 Section 8.1.1 8.1.4

Objectives
This lecture will Introduce the ABAP dictionary as part of SAP Introduce ABAP Open SQL Explore reading DB Tables using ABAP SELECT statements Introduce System Variables and examine how they can be incorporated into an ABAP program Examine program documentation options

BCO5647

The ABAP Dictionary


The ABAP Dictionary administers the database tables. The ABAP dictionary contains current information about a database tables technical attributes. When you look at a table in the ABAP Dictionary, you are looking at the DESCRIPTION of a table in the underlying database.

BCO5647

ABAP Open SQL


In a report program you use ABAP Open SQL to read data from the database.

BCO5647

ABAP Open SQL


Open SQL is SAPs version of Standard SQL. Open SQL statements are converted into database-specific native SQL statements. Open SQL makes you code portable and fast.

BCO5647

Using SELECT to Retrieve Data 1


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

BCO5647

Using SELECT to Retrieve Data 2


Reading several rows using a loop.

BCO5647

Using SELECT to Retrieve Data 3


Reading several rows using an Array Fetch

BCO5647

Reusable Components for Data Retrieval


If reusable components that encapsulate complex data retrieval are available, you should use them. There are four possible techniques:
Calling methods of global classes; Calling methods of business objects; Calling function modules; Including logical databases.

BCO5647

Reading Data from the Database


1. 2. 3. 4. 5. report y234802a. tables: sbook. select * from sbook. write: / sbook-carrid, sbook-fldate. endselect.

In the example above the tables statement:


allocates a table work area called sbook;

gives the program access to the database table: sbook.

The select statement begins a loop. The endselect marks the end of the loop. Code between select and endselect are executed once for each row returned from the database.

BCO5647

10

Using WHERE to limit the Data returned with SELECT


1. 2. 3. 4. 5. 6. report y234802b. tables sbook. select * from sbook where carrid = LH. write: / sbook-carrid, sbook-fldate. endselect.

The where clause allows the program to specify which rows of data are to be returned by the select command.
1. 2. 3. 4. 5. 6. 7. report y234802c. tables sbook. select * from sbook where carrid = LH and connid = 0400. write: / sbook-carrid, sbook-fldate. endselect.

BCO5647

11

Using ORDER BY to sort the Data returned with SELECT

1. 2. 3. 4. 5. 6. 7.

report y234802d. tables spfli. select * from spfli order by cityfrom. write: / spfli-carrid, spfli-connid, spfli-cityfrom. endselect.

The order by clause returns the result of a select loop in ascending order by the nominated field.

BCO5647

12

Working with System Variables


SAPs R/3 system provides a number of system variables which are always available to your program.
All system variables have been defined in the Data Dictionary. All system variables begin with the prefix sy-

For example:
sy-datum sy-uzeit

the current system date. the current time.

Two system variables useful when using the select statement are:
sy-subrc sy-dbcnt

BCO5647

13

Working with System Variables


sy-subrc can be used to test whether any rows from the database table were returned after using a select statement.
If rows were found, the value of sysubrc is 0. If no rows were found, the value of sysubrc is 4.
1. report y234802e. 2. tables sbook. 3. select * from sbook 4. 5. where carrid = LH. write: / sbook-carrid, sbook-fldate.

6. endselect. 7. if sy-subrc ne 0. 8. write / No records found.

9. endif.

BCO5647

14

Working with System Variables


sy-dbcnt can be used to determine the number of rows returned by a select statement. sy-dbcnt can also be used as a loop counter.
1. report y234802f. 2. tables t001. 3. select * from t001. 4. 5. 6. write: / sy-dbcnt, t001-compyname. endselect. write: / sy-dbcnt, records found.

BCO5647

15

Using the SELECT SINGLE statement


The select single statement retrieves one row (record) from the database table.
select single does not start a loop and so there is no endselect statement. A where clause must be included containing the value of a primary key that would uniquely identify one row in the database table.
1. report y234802g. 2. tables t001. 3. select single * from t001

4.

where compycode = A0125.

5. if sy-subrc = 0. 6. 7. write: / t001-compycode, t001-compyname. else.

8.
9. endif.

write: / record not found.

BCO5647

16

Commenting Code and Formal Documentation


An * (asterisk) in column one indicates that the entire line is a comment. (double quotes) anywhere on a line indicates that the remainder of the line is a comment. Formal documentation can be made in the documentation component of the program.
Go to the ABAP Editor: Initial Screen; Click on the Documentation radio button; Press Change; Type in your documentation; Press Save.

BCO5647

17

Commenting Code and Formal Documentation

*----------------------------------------------* * Report Name: y234802h * Author: John Verbatum * Date: February 2007 *----------------------------------------------* * Description: * This program will *----------------------------------------------*

report y234802h.
*----------------------------------------------* * Tables *----------------------------------------------* tables: bsis, kna1, vbrp. Index for G/L Accounts Data in Customer Master Billing: Item Data

*----------------------------------------------* * Variables *----------------------------------------------* data: w_buzei(3) type n, w_monat(2) type n. Line item number Fiscal Period

BCO5647

18

You might also like