Anda di halaman 1dari 18

Lecture 2

Reading Database Tables







BCO5647 Applications Programming Techniques (ABAP)
2 BCO5647
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


3 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.


4 BCO5647
ABAP Open SQL
In a report program you use ABAP Open SQL to read data from the
database.
5 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.


6 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
7 BCO5647
Using SELECT to Retrieve Data 2
Reading several rows using a loop.
8 BCO5647
Using SELECT to Retrieve Data 3
Reading several rows using an Array Fetch
9 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.

10 BCO5647
Reading Data from the Database
1. report y234802a.
2. tables: sbook.
3. select * from sbook.
4. write: / sbook-carrid, sbook-fldate.
5. 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.
11 BCO5647
Using WHERE to limit the Data returned with SELECT
1. report y234802b.
2. tables sbook.
3. select * from sbook
4. where carrid = LH.
5. write: / sbook-carrid, sbook-fldate.
6. endselect.

The where clause allows the program to specify which rows of data are to
be returned by the select command.

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.


12 BCO5647
Using ORDER BY to sort the Data returned with SELECT
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.

The order by clause returns the result of a select loop in ascending order by
the nominated field.
13 BCO5647
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 the current system date.
sy-uzeit the current time.
Two system variables useful when using the select statement are:
sy-subrc
sy-dbcnt


14 BCO5647
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. where carrid = LH.
5. write: / sbook-carrid, sbook-fldate.
6. endselect.
7. if sy-subrc ne 0.
8. write / No records found.
9. endif.



15 BCO5647
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. write: / sy-dbcnt, t001-compyname.
5. endselect.
6. write: / sy-dbcnt, records found.


16 BCO5647
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. write: / t001-compycode, t001-compyname.
7. else.
8. write: / record not found.
9. endif.



17 BCO5647
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.
18 BCO5647
Commenting Code and Formal Documentation
*----------------------------------------------*
* Report Name: y234802h
* Author: John Verbatum
* Date: February 2007
*----------------------------------------------*
* Description:
* This program will
*----------------------------------------------*
report y234802h.
*----------------------------------------------*
* Tables
*----------------------------------------------*
tables: bsis, Index for G/L Accounts
kna1, Data in Customer Master
vbrp. Billing: Item Data
*----------------------------------------------*
* Variables
*----------------------------------------------*
data: w_buzei(3) type n, Line item number
w_monat(2) type n. Fiscal Period

Anda mungkin juga menyukai