ABAP Data Types
- Elementary Data Types
- User Defined Types
Objectives
To introduce :
- ABAP elementary and user defined data types
- Keywords used to declare data items
- Complex record structures
DATA TYPES and DATA OBJECTS
Data types:
- Pure type description
- Do not occupy their own MEMORY space
- Characterize the technical attributes of all data objects that have a particular type.
- Either built-in or defined types.
Predefined ABAP Data Types
Type | Description | Initial Value | Length |
---|---|---|---|
C - Type | Character | Space | 1 - 65535 |
D - Type | Date | '00000000' | 8 |
F - Type | Floating Point | 0.0 | characters |
I - Type | Integer | 0 | 8 bytes |
N - Type | Numeric text | '0' | 4 bytes |
P - Type | Packed decimal | 0 | 1 - 65535 |
T - Type | Time | '000000' | 1 - 16 bytes |
X - Type | Hexadecimal | '00' | 6 characters |
String Type | Variable-length | Space | 1 - 65535 |
Data Objects
- Concrete representation of types.
- Each data object has a particular type & occupies its own memory space.
- Declared with data defining statement
- Statically declared.
- Dynamically generated.
Local Data -Data Type
A data field in ABAP is a variable which cannot be broken up into smaller parts.
- All data fields that are to be used in a program must be defined before their first use.
- Data fields in an ABAP program have a type.
- E.G. You can define a data field student_name of type character and length 25 as:data student_name(25) type c.
ABAP Data Types & Structures
Elementary types supported by ABAP
- Characters
- Numbers
- Date
- Time
- Hexadecimal
Non-elementary types(user defined structures)
- Records
- Internal tables
Character Types
c character
- default type when none is specified
- used for regular text information
- pads rightend 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 leftend with zeros
Number Types
iintegers
- fixed length of 4 bytes
- possible values range from 2-31to 231-1
ppacked
- numbers stored in compressed formats, ie, 2 digits per byte of storage.
- can be used for all types of calculations
ffloating 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
ddate
- fixed length of 8 and internal representation of YYYY/MM/DD
- 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
ttime
- 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
xhexadecimal
- 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
Predefined ABAP Data Types & Attributes
DATA TYPE |
MEANING | INITIAL VALUE |
DEFAULT LENGTH |
PERMITTED LENGTH |
P I F N C D T X |
PACKED NO. INTEGER FLOATING POINT NO. NUMERIC TEXT TEXT DATE(YYYYMMDD) TIME (HHMMSS) HEXADECIMAL |
0 0 '0.0' '00..0' SPACE '00000000' '000000' X'00' |
8 4 8 1 1 8 6 1 |
1-16 - - 1-max 1-max 8 6 1-max |
The ABAP Statement
- Most ABAP statements begins with a reserved keyword, e.G. Data, types, select
- All ends 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 datakeyword
Datavarname[(len)] typetypedef[valueval].
- Data postcode(4) type n value ‘4001’.
- Data counter type i value 1.
- Data price type p decimals 2.
Datavarnamelikeobjname[valueval].
- Where objnameis a previously declared item (or database object)
- Data DOB like sy-datum.
The DataStatement
- Multiple variable declarations can be made in the same data statement by using data:
- Data: DOB like sy-datum, counter type i.
Data Declaration
Variants: 1. Data f. 2. Data f(len). Additions: 1. ... TYPE type 2. ... LIKE f1 3. ... TYPE REF TO cif 4. ... TYPE REF TO DATA 5. ... TYPE LINE OF itabtype 6. ... LIKE LINE OF itab 7. ... DECIMALS n 8. ... VALUE lit 9. ... READ-ONLY |
Example Of Data Declaration
DATA: | WS_COUNTER TYPE I , WS_NAME1(25) , WS_NAME2(25) TYPE C, WS_START_DATE TYPE D, WS_SALES_DOC LIKE VBAK-VBELN, WS_SUM1(5) TYPE P DECIMALS 2. |
TYPES:
|
BEGIN OF X_TYPE1, COUNTER TYPE I , NAME1(25) , SALES_DOC LIKE VBAK-VBELN , END OF X_TYPE1. WS_VAR1 TYPE X_TYPE1. |
The ParametersKeyword
The ConstantsKeyword
- Constants are declared like fields with the addition of the valueclause.
- 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)
Constants
TEXT LITERAL 'ABC' , ' THIS IS A TEXT '.SEQUENCE OF UP TO 255 CHARACTERS ENCLOSED IN QUOTATION MARKS. |
|
NUMBER LITERAL |
|
TYPE P I F N C D T X |
CONSTANT 579,-713 ,'2.17' 917 ,-882 '25.873' , '1.213E15' , '17E-6' , '0.556E7' '778' , '87931' 'AXZ' , 'B123' , 'AB''CD' '19920221' '105743' '0FFF' |
FieldStrings (records or structures)
- Field strings consist of a series of variables grouped together under a common name.
- Field strings are defined using the keywords begin ofand end of.data: begin of customer,id(8) type n,fname(12),lname(15),end of customer.customer-id = ‘12345678’.customer-fname = ‘Christain’.customer-lname = ‘Wong’.write / customer.write: / customer-id,customer-fname,customer-lname.
Records (or Structures)
- Records consist of a fixed number of data objects called componentsof 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.
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.
Rules For Assigning Type Names
- A NAME CAN CONSIST OF UPTO 30 CHARACTERS (Incl. Letters , digits & special characters)
- FOLLOWING CHARACTERS ARE NOT ALLOWED ( ) + . : , < >
- NAME CANNOT CONSIST OF ONLY DIGITS.
RECOMMENDATION :
1.ALWAYS USE A LETTER AS THE FIRST CHARACTER. OTHER CHARACTERS MAY BE LETTERS / DIGITS.
2.JOIN INDIVIDUAL WORDS BY UNDERSCORE.
The TypesKeyword
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.
Type Declaration
Variants:
1. TYPES type.
2. TYPES type(len).
Additions:
1. ... TYPE type1
2. ... LIKE f
3. ... TYPE REF TO cif
4. ... TYPE REF TO DATA
5. ... TYPE LINE OF itabtype
6. ... LIKE LINE OF itab
7. ... DECIMALS n
Example Of Type Declaration
TYPES: TYPES: |
COUNTER TYPE I , NAME1(25) , NAME2(25) TYPE C , START_DATE TYPE D , SALES_DOC LIKE VBAK-VBELN , SUM1(5) TYPE P DECIMALS 2. BEGIN OF X_TYPE1, COUNTER TYPE I , NAME1(25) , START_DATE TYPE D , SALES_DOC LIKE VBAK-VBELN , SUM1(5) TYPE P DECIMALS 2, END OF X_TYPE1. |
The TablesKeyword
- Tables
- 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
More Complex Structures
It is possible to create nestedstructures
- 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’.
Rules For Assigning Field Names
- A NAME CAN CONSIST OF UPTO 30 CHARACTERS (incl. Letters , digits & special characters)
- Following characters are not allowed ( ) + . : , < >
- Name cannot consist of only digits.
- Space is a predefined field
- Same name can not be used for a parameter & a field in an ABAP/4 statement.
Type Specific Output
Type | Standard output length | Output |
P I F N C D T X |
2*len (*) 22 len len 8 6 2* len |
RIGHT-JUSTIFIED RIGHT-JUSTIFIED RIGHT-JUSTIFIED LEFT-JUSTIFIED LEFT-JUSTIFIED LEFT-JUSTIFIED LEFT-JUSTIFIED LEFT-JUSTIFIED |
len = LENGTH OF FIELD IN BYTES.(*) DEPENDS ON LARGEST NUMBER TO BE DISPLAYED.
Type Conversion
Any type can be converted to type C, but the reverse is NOT true.
DO NOT transfer non-numeric values to numeric types.
If the value assigned exceeds the specified length (say len), then the variable will contain:
- First (len) characters in case of type C
- Last (len) characters in case of type N.
Conversion Chart
Conclusion
- 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
Conclusion
- User defined structures are declared using the typeskeyword.
- Identifiers are declared using
- The datakeyword
- The parameterskeyword
- Constants are declared using the constantskeyword.
- The datatype of an identifier can be specified using
- Typespecific type
- Likepreviously defined identifier/dictionary object
Exercise
1.Create a report (‘ZEXC_DATATYPE_NNNNNNNN’) using transaction ‘se38’.
2.Create the following data types:
- X_type1
- char of length 20
- X_type2
- FLTP with 2 decimals
3.Create a data type x_type3 with the following structure:
- X_type1
- X_type2
- Date
- Time
Time allocated: 15 MINS.
Exercise
4.Create variables of types:
- Ws_var1
- x_type1,
- Ws_var2
- x_type2.
5.Create 2 variables of type x_type3 (WS_var3 & WS_var4).
6.Assign values to WS_var1 & WS_var2.
7.Populate WS_var3 with the previous variables and current date and current time.
8.Populate WS_var4 with the values from WS_var3.
Time allocated: 30 MINS.