Sie sind auf Seite 1von 30

SAP - Defining Data in ABAP/4, Part 2

Reference materials
SAP Teach Yourself ABAP in 21 Days-SAMS.pdf

SAP BC ABAP Programming.pdf


http://help.sap.com

Contents
Objectives

Defining Constants
Defining Field Strings Defining Types Structured Types Summary Q&A

Contents
Objectives

Objectives
Use the TABLES statement to define field strings Understand the difference between field strings defined using DATA and using TABLES Understand the TYPES statement and use it to define your own data types

Contents
Defining Constants

Constants
value cannot be changed use the CONSTANTS statement 1 pre-defined constant: SPACE (same as the literal ) Syntax:
constants c1[(l)] [type t] [decimals d] value 'xxx'. constants c1 like cv value 'xxx'.

where:
c1 is the name of the constant. cv is the name of a previously defined constant or variable, or is the name of a field that belongs to a table or structure in the Data Dictionary. (l) is the internal length specification. t is the data type. d is the number of decimal places (used only with type p). 'xxx' is a literal that supplies the value of the constant.

Constants
constants c1(2) type c value 'AA'. constants c2 like c1 value 'BB'. constants error_threshold type i value 5. constants amalgamation_date like sy-datum value '19970305'.

Contents
Defining Field Strings

Field Strings
Field String:
~ structure in DDIC
Defined within ABAP/4 program

2 statements to define field strings:


DATA TABLES

DATA Statement
Syntax:
data: begin of fs1, f1[(l)] [type t] [decimals d] [value 'xxx'], f2[(l)] [type t] [decimals d] [value 'xxx'], ... end of fs1. data begin of fs1. data f1[(l)] [type t] [decimals d] [value 'xxx']. data f2[(l)] [type t] [decimals d] [value 'xxx']. ... [include structure st1.] data end of fs1. data fs1 like fs2.

DATA Statement
where:
fs1 is the field string name. f1 and f2 are the fields (also called components) of the field string. fs2 is the name of a previously defined field string, or is the name of a table or structure in the Data Dictionary. (l) is the internal length specification. t is the data type. d is the number of decimal places (used only with type p). 'xxx' is a literal that supplies a default value. st1 is the name of a structure or table in the Data Dictionary.

DATA Statement
REPORT y_vu_0802. DATA: BEGIN OF totals_1, region(7) VALUE 'unknown', debits(15) TYPE p, count TYPE i, END OF totals_1, totals_2 LIKE totals_1. totals_1-debits = 100. totals_1-count = 10. totals_2-debits = 200. WRITE: / totals_1-region, totals_1-debits, totals_1-count, / totals_2-region, totals_2-debits, totals_2-count.

A Field String Can Contain Another Field String


REPORT y_vu_0803. DATA: BEGIN OF names, name1 LIKE kna1-name1, name2 LIKE kna1-name2, END OF names. DATA: BEGIN OF cust_info, number(10) TYPE n, nm LIKE names, "like a field string END OF cust_info. cust_info-number = 15. cust_info-nm-name1 = 'Jack'. cust_info-nm-name2 = 'Gordon'. WRITE: / cust_info-number, cust_info-nm-name1, cust_info-nm-name2.

Defined Exactly Like a DDIC Table or Structure


REPORT y_vu_0804. DATA: my_lfa1 LIKE lfa1, my_addr LIKE addr.

"like a table in the DDIC "like a structure in the DDIC

my_lfa1-name1 = 'Andrea Miller'. my_lfa1-telf1 = '1-243-2746'. my_addr-land1 = 'CA'.

WRITE: / my_lfa1-name1, my_lfa1-name2, my_addr-land1.

Include DDIC Tables and Structures


REPORT y_vu_0805. DATA BEGIN OF fs1. INCLUDE STRUCTURE lfa1. DATA: extra_field(3) TYPE c, END OF fs1. fs1-lifnr = 12. fs1-extra_field = 'xyz'. WRITE: / fs1-lifnr, fs1-extra_field.

LIKE vs INCLUDE
REPORT y_vu_0806. DATA: BEGIN OF fs1, mylfa1 LIKE lfa1, extra_field(3) TYPE c, END OF fs1. REPORT y_vu_0805. DATA BEGIN OF fs1. INCLUDE STRUCTURE lfa1. DATA: extra_field(3) TYPE c, END OF fs1.

fs1-mylfa1-lifnr = 12. fs1-extra_field = 'xyz'.


WRITE: / fs1-mylfa1-lifnr, fs1-extra_field.

fs1-lifnr = 12. fs1-extra_field = 'xyz'.


WRITE: / fs1-lifnr, fs1-extra_field.

Using a Field String as a Variable of Type CHAR

REPORT y_vu_0807. DATA: BEGIN OF fs1, c1 VALUE 'A', c2 VALUE 'B', c3 VALUE 'C', END OF fs1. WRITE: / fs1-c1, fs1-c2, fs1-c3, / fs1. fs1 = 'XYZ'. WRITE: / fs1-c1, fs1-c2, fs1-c3, / fs1.

Assignment Involving Two Field Strings


REPORT y_vu_0808. DATA: BEGIN OF fs1, c1 VALUE 'A', c2 VALUE 'B', c3 VALUE 'C', END OF fs1, fs2 LIKE fs1. fs2 = fs1. WRITE: / fs2-c1, fs2-c2, fs2-c3.

TABLES Statement
Syntax:
tables fs1.

where:
fs1 is the field string name. A table or structure of the same name must exist in the Data Dictionary.

REPORT y_vu_0809. TABLES lfa1. lfa1-name1 = 'Bugsy'. lfa1-land1 = 'US'. WRITE: / lfa1-name1, lfa1-land1.

TABLES Statement
It defines a field string. It gives the program access to a database table of the same name, if one exists.
REPORT y_vu_0810. TABLES lfa1. SELECT * FROM lfa1 INTO lfa1 ORDER BY lifnr. WRITE / lfa1-lifnr. ENDSELECT.

Contents
Defining Types

Types
to define own data types base on existing data types Syntax:
types t1[(l)] [type t] [decimals d]. types t1 like v1.

where:
t1 is the type name. v1 is the name of a variable previously defined in the program, or is the name of a field that belongs to a table or structure in the Data Dictionary. (l) is the internal length specification. t is the data type. d is the number of decimal places (used only with type p).

TYPES statement
REPORT y_vu_0811. TYPES char2(2) TYPE c. DATA: v1 TYPE char2 VALUE 'AB', v2 TYPE char2 VALUE 'CD'.
WRITE: v1, v2.

Make Your Code Clearer and Easier to Read


REPORT y_vu_0812. TYPES: dollars(16) TYPE p DECIMALS 2, lira(16) TYPE p DECIMALS 0. "italian lira have no decimals DATA: BEGIN OF american_sums, petty_cash TYPE dollars, pay_outs TYPE dollars, lump_sums TYPE dollars, END OF american_sums, BEGIN OF italian_sums, petty_cash TYPE lira, pay_outs TYPE lira, lump_sums TYPE lira, END OF italian_sums.

american_sums-pay_outs = '9500.03'. "need quotes when literal contains a decimal italian_sums-lump_sums = 5141.
WRITE: / american_sums-pay_outs, / italian_sums-lump_sums.

Contents
Structured Types

Structured Types
Structured Type: a user-defined type with the definition of a field string to Reduce Redundancy and Make Maintenance Easier

REPORT y_vu_0813. TYPES: BEGIN OF address, street(25), city(20), region(7), country(15), postal_code(9), END OF address. DATA: customer_addr TYPE address, vendor_addr TYPE address, employee_addr TYPE address, shipto_addr TYPE address. customer_addr-street = '101 Memory Lane'. employee_addr-country = 'Transylvania'.

Contents
Summary

Summary
Field strings:
like DDIC structures define using DATA or TABLES statement
DATA: local or global visibility TABLES: global and external visibility

TYPES statement
define own data types User-defined types reduce redundancy and make maintenance easier.

Thank you!
Questions & Answers

Das könnte Ihnen auch gefallen