Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields
Objectives
To introduce : ABAP elementary and user defined data types keywords used to declare data items complex record structures the use of internal tables in ABAP declaring an internal table populating fields of an internal table the ABAP data dictionary domains, elements & tables integrity constraints
ABAP Data Types
Elementary Data Types User Defined Types
ABAP Data Types & Structures
Elementary Types
supported by ABAP characters numbers Date Time Hexadecimal
Non- Elementary Types
Structures ) records internal tables
(
User Defined
Character Types
c character default type when none is specified used for regular text information pads right end with spacesn numeric text contains strings of digits used for numbers that are used for identification and as sort criteria for internal tables house numbers, postcodes, phone numbers, ... pads left end with zeros
Number Types
i integers fixed length of 4 bytes possible values range from 2 -31 to 2 31 -1 p packed numbers stored in compressed formats, ie, 2 digits per byte of storage. can be used for all types of calculations f floating point possible range from 1E-307 to 1E307 can be used in all types of calculations but beware of rounding errors. (Don’t use in eq condition tests)
Date Type
d date fixed length of 8 and internal representation of YYYYMMDD several output formats supported set default output format in your User Profile/Defaults can specify different/specific output format when writing value out supports date arithmetic
Time Type
t time fixed length of 6 and format HHMMSS several output formats supported can specify different/specific output format when writing value out supports time arithmetic
Hexadecimal Type
x hexadecimal values in hexadecimal fields are stored in binary (bit stream) format eg ‘F089’ would be stored as 1111000010001001 2 hex digits are stored in 1 storage byte
Elementary Types & Default Lengths and Values
Type Length Initial Value
t x i c n f p d 1 1 4 8 8 8 6 1 space ‘0’ 0 0 0.0
‘00000000’ ‘000000’ X’00’
The ABAP Statement
most ABAP statements begins with a reserved keyword, e.g.
data
,
types
,
select
all end with a period (.) can combine a sequence of statements starting with the same keywords into a single statement using colon and comma (for most keywords)
The
data
keyword
variables are declared using the
data
data
varname [(len)] type typedef keyword [value val] .
data postcode(4) type n value ‘4001’.
data counter type i value 1.
data price type p decimals 2.
data
varname where object) objname
like
objname [value val] .
is a previously declared item (or database data DOB like sy-datum.
The
data
statement
multiple variable declarations can be made in the same data statement by using
data:
data: DOB like sy-datum, counter type i.
The
parameters
keyword
PARAMETERS TYPE [DEFAULT ].
PARAMETERS: P1 TYPE P, P2(6) TYPE C DEFAULT ‘ITB255’.
X
P1 P2
ITB255 Selection Screen
The
constants
keyword
Constants are declared like fields with the addition of the value clause.
constants: max_counter type i value 9999.
A constant cannot be changed trying to change the value of a constant will result in a runtime error (if the change can only be detected at runtime, ie, if the constant is used as an actual parameter)
Records (or Structures)
records consist of a fixed number of data objects called components of the record declared using data begin of and data end of data: begin of student, stnum(8) stname(25) stDOB stphone(12) end of student.
type n, type c, like sy-datum, type n,
Working With Records
components of records are referenced by
recordname-componentname
data IT_student like student.
student-stnum = ‘12345678’.
student-stname = ‘F.Jones’.
student-stDOB = ‘19790623’.
student-stphone = ‘3221 1278’.
move student to IT_student.
The
types
keyword
used to create a user defined type types: begin of student_type, stnum(8) type n, stname(25) stDOB type c, like sy-datum, stphone(12) type n, end of student_type.
data: student_rec type student_type, student_tab type student_type occurs 0.
The
tables
keyword
tables
, e.g. tables spfli Creates a structure in the program work area with the same: name row structure field names, datatype, sequence as the referenced database table, structure or view
Internal Tables
used as snapshots of database tables containers for volatile data exist only at runtime, (unlike database tables) consist of any number of records declared using occurs
n
data student_tab like student occurs 0.
data student_tab like student occurs 0 with header line.
note that it is not necessary to declare the exact size of the table: SAP’s memory management allows the table to be ‘infinitely’ extensible.
Internal Tables – with header line
a record structure of the same name is automatically created Program
Work Area
R/3 Database
Internal Tables – without header line
need to define a record structure explicitly in your abap program Program
Work Area
R/3 Database
Internal Tables - header lines
advantages of header lines convenient declaration table and header line structure declared in same statement ease of use don’t have to explicitly move data into the work area structure and then append the work area structure to the table some statements require a table with header line disadvantages of header lines performance - I/O is faster for tables without header line cannot use in embedded structure
Internal Tables - header lines
default declaration is line
without
a header to declare a table with a header line data: itab {type tabtype | like occurs n
with header line
objname } data book_tab like sbook occurs 0 with
header line.
More Complex Structures
It is possible to create nested structures a structure can contain another structure a record can contain an internal table as one of its components a table can contain another table, etc types: begin of addr_type, city(25), street(30), end of addr_type, begin of person, name(25), address type addr_type, end of person.
data: emp type person.
emp-address-city = ‘Sydney’.
The Data Dictionary
Domains Data Elements Tables & Structures
3 Levels of the Dictionary
Tables or Structures composed of one or more fields Data Elements each field refers to a meaning of the field data element that describes the Domain determines the technical properties of the field data type and size (including number of decimal places) allowed data values output characteristics
Elements and Domains
Domain provides the technical description Element determines the role played create a domain called ID_Number create elements Student_ID and Staff_ID based on the ID_Number domain
Referential Integrity Check Referential Integrity Check Data Element Definition Data Element Definition
Field Information
Domain Definition Define the text to be displayed on screen or report
Range/Value Integrity Check
Integrity Checking
Domain Range/Value Integrity Checks value table only values contained in the value table can be entered in fields referring to this domain fixed values only values that match a value in the user specified list of admissible values can be entered in fields referring to this domain
Integrity Checking
Referential Integrity Checking check table foreign key values must match an entry in the specified check table check tables bound to input fields on data entry screens position the cursor on input field and press F4 to get a list of permissible values the default check table for a field is the value table of the underlying domain from the “Dictionary: Table/Structure: Display Fields” screen, Select GoTo…Foreign Keys (F8)
Foreign Key value must match Primary Key value of the check table Cardinality Ratio of Foreign Key Scroll Through Other Defined Foreign Keys
Viewing the Contents of a Database Table
From the Data Dictionary Click on the Table Radio Button & enter the Table Name From the Dictionary Table: Table/Structure: Display Fields screen choose Utilities…Table Contents Data Browser (from main menu item Overview ) Enter the Table Name Choose Table…Table Contents or press Enter Fill in the appropriate Selection Screen entries Click on the Execute icon on the application toolbar, (or press F8)
Sort Buttons Table Name Number of Rows Selected
Conclusion
This lecture covered aspects of defining identifiers in an ABAP program the data dictionary In particular we covered the basic data types supported by ABAP character, number, integer, float, packed, date, time, hexadecimal We also discussed user defined structures records, tables
Conclusion
User defined structures are declared using the types keyword.
Identifiers are declared using the data keyword the parameters keyword Constants are declared using the constants keyword.
The datatype of an identifier can be specified using
type like
specific type previously defined identifier/dictionary object
Conclusion
Structures records defined by data begin of / data
end of
internal tables defined using occurs complex structures can be created by nesting records and internal tables
Conclusion
The second half of the lecture covered Data Dictionary Organization of Data in the Dictionary Domain Elements Tables/Structures Using the Dictionary to see the definitions of Domains, Elements, Tables the contents of Tables How the Dictionary maintains Value and Referential Integrity
Related Reading
Online Help R/3 Library ...Basis Components ... ABAP Workbench BC ABAP Users Guide The ABAP Programming Language … Basic Statements … Declaring Data … Creating Data Objects and Data Types The DATA Statement BC ABAP Dictionary …Tables BC ABAP Dictionary … Data Elements BC ABAP Dictionary … Domains