Title: MIS 3020 ABAP Programming
1MIS 3020 ABAP Programming
- Lecture 2
- Elementary User Defined Types
- Domains, Elements, Variables/Fields
2Objectives
- 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
3ABAP Data TypesElementary Data TypesUser
Defined Types
4ABAP Data Types Structures
- Elementary Types supported by ABAP
- characters
- numbers
- Date
- Time
- Hexadecimal
- Non- Elementary Types (User Defined Structures)
- records
- internal tables
5Character Types
- c character
- default type when none is specified
- used for regular text information
- pads right end with spaces
- n 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
6Number Types
- i integers
- fixed length of 4 bytes
- possible values range from 2-31 to 231-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. (Dont use in eq
condition tests)
7Date 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
8Time 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
9Hexadecimal 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
10Elementary Types Default Lengths and Values
- Type Length Initial Value
- c 1 space
- n 1 0
- i 4 0
- p 8 0
- f 8 0.0
- d 8 00000000
- t 6 000000
- x 1 X00
11The 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)
12The data keyword
- variables are declared using the data keyword
- data varname(len) type typedef value val.
- data postcode(4) type n value 4001.
- data counter type i value 1.
- data price type p decimals 2.
- data varname like objname value val.
- where objname is a previously declared item (or
database object) - data DOB like sy-datum.
13The data statement
- multiple variable declarations can be made in the
same data statement by using data - data DOB like sy-datum, counter type i.
14The parameters keyword
PARAMETERS ltfieldgt TYPE lttypegt DEFAULT ltvaluegt.
PARAMETERS P1 TYPE P,
P2(6) TYPE C DEFAULT ITB255.
X
P1
P2
ITB255
Selection Screen
15The 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)
16Records (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) type n,
- stname(25) type c,
- stDOB like sy-datum,
- stphone(12) type n,
- end of student.
17Working 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.
18The types keyword
- used to create a user defined type
- types begin of student_type,
- stnum(8) type n,
- stname(25) type c,
- stDOB like sy-datum,
- stphone(12) type n,
- end of student_type.
- data student_rec type student_type,
- student_tab type student_type occurs 0.
19The tables keyword
- tables lttablestructureviewgt, 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
20Internal 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 SAPs memory management
allows the table to be infinitely extensible.
21Internal Tables with header line
a record structure of the same name is
automatically created
Program Work Area
R/3 Database
22Internal Tables without header line
need to define a record structure explicitly
in your abap program
Program Work Area
R/3 Database
23Internal Tables - header lines
- advantages of header lines
- convenient declaration
- table and header line structure declared in same
statement - ease of use
- dont 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
24Internal Tables - header lines
- default declaration is without a header line
- to declare a table with a header line
- data itab type tabtype like objname
occurs n with header line - data book_tab like sbook occurs 0 with header
line.
25More 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.
26The Data DictionaryDomainsData ElementsTables
Structures
273 Levels of the Dictionary
- Tables or Structures
- composed of one or more fields
- Data Elements
- each field refers to a data element that
describes the meaning of the field - Domain
- determines the technical properties of the field
- data type and size (including number of decimal
places) - allowed data values
- output characteristics
28Elements 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
29Referential Integrity Check
Referential Integrity Check
Data Element Definition
Data Element Definition
30Field Information
31Domain Definition
Define the text to be displayed on screen
or report
32Range/Value Integrity Check
33Integrity 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
34Integrity 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 GoToForeign Keys (F8)
35Foreign Key value must match
Primary Key value of the check table
Cardinality Ratio of Foreign Key
Scroll Through Other Defined Foreign Keys
36Viewing 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 UtilitiesTable
Contents - Data Browser (from main menu item Overview)
- Enter the Table Name
- Choose TableTable Contents or press Enter
- Fill in the appropriate Selection Screen entries
- Click on the Execute icon on the application
toolbar, (or press F8)
37Number of Rows Selected
Table Name
Sort Buttons
38Conclusion
- 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
39Conclusion
- 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 specific type
- like previously defined identifier/dictionary
object
40Conclusion
- 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
41Conclusion
- 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
42Related 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