Anda di halaman 1dari 19

Data Declaration

SAP AG 2005

SAP AG 1
Data Objects: Contents/Objectives

Contents
Data Objects and Types
Operations on Data Objects

Objectives
At the end of this presentation, you will be able to:
Describe various ways of defining data
Understand basic operations on data in ABAP programs

SAP AG 2006

SAP AG 2
Data Objects

Content:

Definition of Data Objects and Types


Operations on Data Objects

SAP AG 2006

SAP AG 3
Data Objects in Programs

Data

ABAP program
Elementary data objects
Operations
on Structures
data objects

Internal tables

Data

SAP AG 2006

There are three basic types of data objects that can be used in ABAP programs:
y Elementary data objects (fields),
y structures
y Internal tables.

SAP AG 4
Using Types

Types describe the attributes of

Input / output fields


Interface parameters
for functions/methods
Interface parameters
Data objects for subroutines

ce
r fa n
te tio s
ABAP In c
n le
source Fu odu
code M
Interface
Function
Subroutine Group

ABAP program

SAP AG 2006

Types describe the attributes of


y Input and output fields in screens
y Data objects
y Interface parameters: Type checks are performed each time a function or subroutine is called according
to how the interface parameter is typed. Type conflicts are already identified during the editing process
and appropriate syntax errors displayed.
For example, if you have defined a field of type d (date) the system will check that 8 numbers are
entered, and that these represent a valid value for day, mounth and year.
In order to avoid errors when passing data between different programs and thus to be able to ensure data
consistency, most data is defined with reference to the ABAP Dictionary (SE11).
When you create a new data object in SE11 you will be prompted a popup which asks you to specify
wheather you like to create a data element, a structure or an internal table.
It is also possible to define the type of a data object without reference to the data dictionary. In this case
you must specify all the technical attributes manually, e.g. the statement DATA: my_par (2) TYPE c.
would define a field of type character with length 2.
Apart from possible incompatabilities to other programs another disadvantage of local types is that you do
not get any semantic attributes (like F1-documentaion, search helps, etc.).

SAP AG 5
Defining Data Objects

Predefined Predefined
types ABAP types
What is the structure How is the data
of the type? stored?
By field:
The type attributes of Predefined How is it
the data object must be ABAP type interpreted?
completely specified Length
d t
x
n xstring

i c
string
f
p
DATA: <varname> TYPE <type>.

Global
types in
the ABAP
Dictionary
DATA: <varname> LIKE <data object>.

SAP AG 2006

All data objects can be defined using the ABAP keyword DATA. This slide summarizes the syntax of the
data statement.
Assign data object types by referring your object to either a built-in ABAP type, a user defined type, or an
ABAP Dictionary object.
If a variable v2 refers to variable v1 using the addition LIKE (DATA v2 LIKE v1), then v2 inherits its
type from v1.
Up to Release 4.0B, you could only refer to Dictionary types using LIKE. Only structure fields could be
used as elementary types up to that point. Only flat structures were provided as structure types.

SAP AG 6
GlobalTypes in the ABAP Dictionary

ABAP Dictionary:

Global types Database table


selections

Data element

Structure type
Transparent Table
Structures
SCARR:

Line type
Key fields
Table type Secondary indexes
... SCARR

SAP AG 2006

You define global types and manage the descriptions of database tables in the ABAP Dictionary. You
have the following options for global types:
y Elementary types are called data elements. They contain a complete description of the technical
attributes of an elementary field, along with semantic information.
y Structure types are usually known as structures. They can consist of elementary components.
Alternatively, each component can itself have a structured type. This allows you to construct nested
structure types of any depth you want. Note that you can also use the line type of a transparent table
(that is one defined as a database table) as a structured type.
y Table types are types used to define internal tables.
ABAP programs often read complete rows from a database table. Therefore it is desireable to have an easy
possibility to define data objects in programs so that they have exactly the same line type as a database
table.
Note that a statement: DATA <object> TYPE <DBtable> creates a structure, that means a workarea with
just one line and not an internal table. Only the line type is taken over from the database tables and no
other attributes of the DB-table.

SAP AG 7
Defining Fields in ABAP Programs

ABAP Dictionary:

Field of structure or table Data element


scarr-carrid s_carr_id

Field Definition

1st_car ABAP
program

ABAP Source Code


DATA: 1st_car(2) TYPE c.
DATA: 2nd_car TYPE s_carr_id.
DATA: 3rd_car TYPE scarr-carrid

3rd_car 2nd_car

SAP AG 2006

Generally, data objects are typed using either a complete local program type or a complete global type.
Double-click the name of the type to display its definition. For local program types this means: Navigate
to the line in the source codes where the type has been defined. For global program types this means:
Navigate to the Dictionary and display the global type.
You can use the complete predefined ABAP types directly, to provide a type for variables. If you do so,
double-clicking on the type after the TYPE statement has no effect. For more information on pre-defined
types, refer to the keyword documentation on TYPES or DATA.
The following predefined ABAP types are complete:
y d Date: (YYYYMMDD)
y t Time: HHMMSS)
y i Integer
y f Floating Point Number
y string character String (string, of variable length)
y xstring byte sequence (heXadecimal string, of variable length)
You must define the length for these types.
y c Character
y n Numeric text (Numeric Character)
y x Byte (heXadecimal)
y p Packed number (= Binary Coded Decimals). You must enter the number of decimal places.
SAP AG 8
Structures with a Dictionary Type Reference

ABAP Dictionary:

Structure Transparent table


sbc400_carrier scarr

Structure Definition
ABAP
sbc400_carrier
program

ABAP Source Code


TABLES sbc400_carrier.

DATA: wa_scarr TYPE scarr.


scarr.

wa_scarr

SAP AG 2006

You can define structured data objects (also called structures) in ABAP. This allows you to combine
variables that belong together into one object. Structures can be nested. This means that other structures or
even tables can be sub-objects of your original structure.
There are two different kinds of structures in ABAP programs:
y Structures defined using
DATA <name> TYPE <structure_type>.
DATA <name> TYPE <DB-Table>.
These kinds of structures serve as the target fields for database accesses or for calculations performed
locally within the program. You can declare these types of structures either in the ABAP Dictionary or
locally within your program. For more information on how to declare local structures, refer to the
keyword documentation on TYPES.
y Structures defined using
TABLES <ABAP-Dictionary-Structure>.
The TABLES statement defines a structure that has the same name as a database table. Unlike objects
that are defined via the data statement, the declaration of a structure with the TABLES keywords allows
to share the contents with other programs. So if a structure is defined with the tables statement in two
programs it does not have to be passed explicitly in the interface between the two programs.
y In the object list of a program all objects defined with TABLES statement are displayed under the node
Dictionary Structures.

SAP AG 9
Example: Dictionary Structure Type SBC400FOCC

ABAP Dictionary:
Structure: SBC400FOCC active
Short text: Percentage Occupancy of Flights

Component Component type Dtyp Length Dec.Pl. Short text


CARRID S_CARR_ID CHAR 3 0 Airline ID

CONNID S_CONN_ID NUMC 4 0 Flight connection code

FLDATE S_DATE DATS 8 0 Flight date

SEATSMAX S_SEATSMAX INT4 10 0 Maximum capacity

SEATSOCC S_SEATSOCC INT4 10 0 Occupied seats

PERCENTAGE S_FLGHTOCC DEC 5 2 Percentage Occupancy


of Flights

SAP AG 2006

Each component is assigned to a structure type. The following information is stored for each component
y Component name: You can choose any name you want; there are no naming conventions. If the
component is given the type of a data element that has a default name assigned to it, you should use this
default value.
y Component type: Generally, a data element is used to provide the type. If so, the component inherits all
the type attributes of this data element. The type and length are displayed in the structure definition.
y Short text: describes the component.
Keep the following in mind, if you want to create a structure in the Dictionary:
y The name of the structure must be in the customer namespace.
y Currency fields usually have the type CURR. You must enter the associated currency field (type
CUKY) as a reference field.
y Lengths, weights, and other sizes measured in units generally have the type QUAN. You must enter the
associated unit field (type UNIT) as a reference field.
To find out the rules on mapping predefined Dictionary types and predefined ABAP types, see the SAP
Library, under Basis-> ABAP Workbench-> BC-ABAP Dictionary-> Data types in the ABAP Dictionary-
> Mapping of ABAP data types

SAP AG 10
Internal Tables with a Dictionary Type Reference

ABAP Dictionary:

Table Type
sbc400_t_scarr

Structure Definition
ABAP
sbc400_t_scarr
program

ABAP Source Code

DATA: it_scarr TYPE sbc400_t_scarr.

it_scarr

SAP AG 2006

SAP AG 11
Data Objects

Content:

Definition of Data Objects and Types


Operations on Data Objects

SAP AG 2006

SAP AG 12
Copying and Initializing Variables

Data declarations:
CONSTANTS c_qf TYPE s_carr_id VALUE 'QF'.
DATA: gd_carrid1 TYPE s_carr_id,
gd_carrid2 TYPE s_carr_id VALUE 'LH',
counter type I.
gd_carrid1 gd_carrid2 counter
Program start c_qf QF LH 0000

MOVE c_qf TO gd_carrid1. QF LH 0000

gd_carrid2 = gd_carrid1. QF QF 0000

ADD 1 TO counter. QF QF 0001

CLEAR: gd_carrid1,
CLEAR
gd_carrid2,
counter. 0000

ABAP program

ABAP runtime system

Time
SAP AG 2006

When a program is started, the program context is loaded into a storage area of the application server and
made available for all the data objects.
Each elementary field comes as standard with an initial value appropriate to its type. You can set an start
value for an elementary field yourself using the VALUE addition. After VALUES you may only specify a
fixed data object.
You can copy the field contents of a data object to another data object with the MOVE statement. If the
two data objects have different types, the type is automatically converted if there is a conversion rule. If
you want to copy the field contents of variable var1 to a second variable var2, you can choose one of two
syntax variants:
y MOVE var1 TO var2.
y var2 = var1.
You can find detailed information about copying and about the conversion rules in the keyword
documentation for MOVE or in the BC402 training course.
The CLEAR statement resets the field contents of a variable to the initial value for the particular type. You
can find detailed information about the initial values for a particular type in the keyword documentation
about CLEAR.

SAP AG 13
SELECT Data from DB (Overview)

Which columns? SELECT <result> FROM <table> Which table(s)?


INTO <destination> Where to?

WHERE <condition> Which lines?

Single line
} Multiple lines
}
Specific
column

SAP AG 2006

You use the Open SQL statement SELECT to read data from the database.
Underlying the SELECT statement is a complex logic that allows you to access many different types of database
table.
The statement contains a series of clauses, each of which has a different task:
y The SELECT clause specifies
- Whether the result of the selection is to be a single line or several lines.
- Which fields should be included in the result.
- Whether the result may contain two or more identical lines.
- You can restrict the selected data to a list of table fields (e.g. carrid connid fldate ), thus reading only parts
of the table row. Alternatively you can select a complete record from the database table if you write * as the
result:
- SELECT SINGLE carrid connid fldate FROM scarr INTO wa_scarr WHERE carrid = pa_car.
SELECT SINGLE * FROM scarr INTO wa_scarr WHERE carrid = pa_car.
- The INTO clause specifies the internal data object in the program into which you want to place the selected
data.
- The FROM clause specifies the source of the data (database table or view).
- The WHERE clause specifies conditions that selection results must fulfill. Thus, it actually determines what
lines are included in the results table.
- For information about other clauses, refer to the keyword documentation in the ABAP Editor for the SELECT
statement.

SAP AG 14
Reading a Single Record

ABAP program

Data objects pa_car


Database
wa_scarr
table
SCARR
ABAP processing block
SELECT SINGLE *
FROM scarr
INTO wa_scarr
INTO
WHERE
WHERE carrid = pa_car.
IF sy-subrc = 0.
...
Database scarr
interface

ABAP runtime system

SAP AG 2006

The SELECT SINGLE * statement allows you to read a single record from a database table. To ensure
that the entry you read is unique, all the key fields must be filled by the WHERE clause. The * informs the
database interface it should read all columns in that line of the database table. If you only want to read
certain columns, you can insert a field list instead.
The name of a structure to which you want the database interface to copy a data record is inserted after the
INTO clause. The structure should have a structure identical to the columns of the database table being
read and be left-justified.
If you use the CORRESPONDING FIELDS OF addition in the INTO clause, you can fill the target work
area component by component. The system only fills those components that have identical names to
columns in the database table. If you do not use this addition, the system fills the work area from the left-
hand end without any regard for its structure.
If the system finds a table entry matching your conditions, SY-SUBRC has the value 0.
The SINGLE addition tells the database that only one line needs to be read. The database can then
terminate the search as soon as it has found that line. Therefore, SELECT SINGLE produces better
performance for single-record access than a SELECT loop if you supply values for all key fields.

SAP AG 15
Addressing Fields in Structures

wa_scarr ABAP
mandt carrid carrname currcode program

Fields in structures are


always addressed
ABAP source code
using
<Structure>-<Fieldname>
DATA: wa_scarr TYPE scarr.

wa_scarr-carrid
wa_scarr-carrid = 'LH'.

SELECT SINGLE * FROM scarr


INTO wa_scarr
WHERE carrid = wa_scarr-carrid.
WRITE:/ wa_scarr-carrid
wa_spfli-carrid,
wa_spfli-carrname.
wa_scarr-carrname

SAP AG 2006

Fields of a structure are always addressed using <Structure>-<Field_name>.


This means that you should never use a hyphen in a variable name. The hyphen character is reserved for
separating the structure name and the field name.

SAP AG 16
Evaluating Field Contents

CASE <data object 1>.


WHEN <data object 2>.
Statements

WHEN <data object 3> OR <data object 4>.


Statements

WHEN OTHERS.
IF <logical expression>.
Statements
Statements

ENDCASE. ELSEIF <logical expression>.


Statements

ELSEIF <logical expression>.


Statements
ELSE.
Statements

ENDIF.

SAP AG 2006

IF and CASE statements allow you to make case distinctions:


CASE ... ENDCASE:
y Only one of the sequences of statements is executed.
y The WHEN OTHERS statement is optional.
IF ... ENDIF:
y The logical expressions that are supported are described in the keyword documentation for the IF
statement.
y The ELSE and ELSEIF statements are optional.
y If the logical expression is fulfilled, the following sequence of statements is executed.
y If the logical expression is not fulfilled, the ELSE or ELSEIF section is processed. If there is no ELSE
or no further ELSEIF statement, the program continues after the ENDIF statement.
y You can include any number of ELSEIF statements between IF and ENDIF. A maximum of one of the
sequences of statements will be executed.

SAP AG 17
Data Declarations: Summary

You are now able to:


Describe various ways of defining data

Understand basic operations on data in ABAP programs

SAP AG 2006

SAP AG 18
Copyright 2006 SAP AG. All Rights Reserved

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained
herein may be changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors.
Microsoft, Windows, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.
IBM, DB2, DB2 Universal Database, OS/2, Parallel Sysplex, MVS/ESA, AIX, S/390, AS/400, OS/390, OS/400, iSeries, pSeries, xSeries, zSeries, z/OS, AFP,
Intelligent Miner, WebSphere, Netfinity, Tivoli, and Informix are trademarks or registered trademarks of IBM Corporation.
Oracle is a registered trademark of Oracle Corporation.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc.
HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology.
Java is a registered trademark of Sun Microsystems, Inc.
JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape.
MaxDB is a trademark of MySQL AB, Sweden.
SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver, and other SAP products and services mentioned herein as well as their respective logos are
trademarks or registered trademarks of SAP AG in Germany and in several other countries all over the world. All other product and service names mentioned are
the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary.

The information in this document is proprietary to SAP. No part of this document may be reproduced, copied, or transmitted in any form or for any purpose without
the express prior written permission of SAP AG.
This document is a preliminary version and not subject to your license agreement or any other agreement with SAP. This document contains only intended
strategies, developments, and functionalities of the SAP product and is not intended to be binding upon SAP to any particular course of business, product strategy,
and/or development. Please note that this document is subject to change and may be changed by SAP at any time without notice.
SAP assumes no responsibility for errors or omissions in this document. SAP does not warrant the accuracy or completeness of the information, text, graphics, links,
or other items contained within this material. This document is provided without a warranty of any kind, either express or implied, including but not limited to the
implied warranties of merchantability, fitness for a particular purpose, or non-infringement.
SAP shall have no liability for damages of any kind including without limitation direct, special, indirect, or consequential damages that may result from the use of
these materials. This limitation shall not apply in cases of intent or gross negligence.
The statutory liability for personal injury and defective products is not affected. SAP has no control over the information that you may access through the use of hot
links contained in these materials and does not endorse your use of third-party Web pages nor provide any warranty whatsoever relating to third-party Web pages.

SAP AG 2006

SAP AG 19

Anda mungkin juga menyukai