42
MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Embed Size (px)

Citation preview

Page 1: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

MIS 3020 ABAP Programming

Lecture 2Elementary & User Defined Types

Domains, Elements, Variables/Fields

Page 2: MIS 3020 ABAP Programming 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

Page 3: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

ABAP Data Types

Elementary Data TypesUser Defined Types

Page 4: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

ABAP Data Types & Structures

Elementary Types supported by ABAP characters numbers Date Time Hexadecimal

Non- Elementary Types (User Defined Structures) records internal tables

Page 5: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Character 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

Page 6: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Number 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. (Don’t use in eq condition tests)

Page 7: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 8: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 9: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 10: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Elementary Types & Default Lengths and ValuesType Length Initial Valuec 1 spacen 1 ‘0’i 4 0p 8 0f 8 0.0d 8‘00000000’t 6 ‘000000’x 1 X’00’

Page 11: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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)

Page 12: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 13: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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.

Page 14: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

The parameters keyword

PARAMETERS: P1 TYPE P, P2(6) TYPE C DEFAULT ‘ITB255’.

PARAMETERS <field> TYPE <type> [DEFAULT <value>].

X

P1

P2 ITB255

Selection Screen

Page 15: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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)

Page 16: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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) type n, stname(25) type c, stDOB like sy-datum, stphone(12) type n,end of student.

Page 17: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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.

Page 18: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

The types keyword

used to create a user defined typetypes: 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.

Page 19: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

The tables keyword

tables <table|structure|view>, 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

Page 20: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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.

Page 21: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Internal Tables – with header line

Program Work Area

R/3 Database

a record structure of the samename is automatically created

Page 22: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Internal Tables – without header line

Program Work Area

R/3 Database

need to define a recordstructure explicitly inyour abap program

Page 23: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 24: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Internal 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 linedata book_tab like sbook occurs 0 with

header line.

Page 25: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 26: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

The Data Dictionary

DomainsData ElementsTables & Structures

Page 27: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

3 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

Page 28: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 29: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Data ElementDefinition

Referential Integrity Check

Data ElementDefinition

Referential Integrity Check

Page 30: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Field Information

Page 31: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Define the text to be displayed on screen orreport

Domain Definition

Page 32: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Range/Value Integrity Check

Page 33: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 34: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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)

Page 35: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Foreign Key value must match

Primary Key value of the check table

Cardinality Ratio of Foreign Key

Scroll Through Other Defined Foreign Keys

Page 36: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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)

Page 37: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

Table Name

Number of Rows Selected

Sort Buttons

Page 38: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 39: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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 specific type like previously defined identifier/dictionary object

Page 40: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 41: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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

Page 42: MIS 3020 ABAP Programming Lecture 2 Elementary & User Defined Types Domains, Elements, Variables/Fields

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