Lecture02 abap on line

18
Lecture 2 Reading Database Tables BCO5647 Applications Programming Techniques (ABAP)

Transcript of Lecture02 abap on line

Page 1: Lecture02 abap on line

Lecture 2Reading Database Tables

BCO5647 Applications Programming Techniques (ABAP)

Page 2: Lecture02 abap on line

2BCO5647

Readings & Objectives

Readings

Keller & Kruger Chapter 8Section 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

Page 3: Lecture02 abap on line

3BCO5647

The ABAP Dictionary

The ABAP Dictionary administers the database tables.

The ABAP dictionary contains current information about a database table’s 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.

Page 4: Lecture02 abap on line

4BCO5647

ABAP Open SQL

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

Page 5: Lecture02 abap on line

5BCO5647

ABAP Open SQL

Open SQL is SAP’s version of Standard SQL.

Open SQL statements are converted into database-specific native SQL statements.

Open SQL makes you code portable and fast.

Page 6: Lecture02 abap on line

6BCO5647

Using SELECT to Retrieve Data 1

The SELECT clause describes whether the result of the selection will comprise severallines 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 tobe placed.

The WHERE clause defines the conditions that the selection results must fulfil.

Reading by Single Record ACCESS

Page 7: Lecture02 abap on line

7BCO5647

Using SELECT to Retrieve Data 2

Reading several rows using a loop.

Page 8: Lecture02 abap on line

8BCO5647

Using SELECT to Retrieve Data 3

Reading several rows using an Array Fetch

Page 9: Lecture02 abap on line

9BCO5647

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.

Page 10: Lecture02 abap on line

10BCO5647

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

Code between select and endselect are executed once for each row returned from the database.

Page 11: Lecture02 abap on line

11BCO5647

Using WHERE to limit the Data returned with SELECT

1. report y234802b.2. tables sbook.3. select * from sbook4. 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 sbook4. where carrid = ’LH’5. and connid = ’0400’.6. write: / sbook-carrid, sbook-fldate.7. endselect.

Page 12: Lecture02 abap on line

12BCO5647

Using ORDER BY to sort the Data returned with SELECT

1. report y234802d.2. tables spfli.3. select * from spfli4. 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 bythe nominated field.

Page 13: Lecture02 abap on line

13BCO5647

Working with System Variables

SAP’s R/3 system provides a number of system variables which are alwaysavailable 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-subrcsy-dbcnt

Page 14: Lecture02 abap on line

14BCO5647

Working with System Variables

sy-subrc can be used to test whether any rows from the database tablewere returned after using a select statement.

If rows were found, the value of sy–subrc is 0.

If no rows were found, the value of sy–subrc 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.

Page 15: Lecture02 abap on line

15BCO5647

Working with System Variables

sy-dbcnt can be used to determine the number of rows returned by a selectstatement.

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

Page 16: Lecture02 abap on line

16BCO5647

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

Page 17: Lecture02 abap on line

17BCO5647

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 isa comment.

Formal documentation can be made in the documentation component of theprogram.

Go to the ABAP Editor: Initial Screen; Click on the Documentation radio button; Press Change; Type in your documentation; Press Save.

Page 18: Lecture02 abap on line

18BCO5647

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