Upload
bakkalibilal
View
28
Download
0
Embed Size (px)
DESCRIPTION
Chapter 04_ABAP Data Declarations
Citation preview
IBM Global Business Services
© IBM Corporation 2013ABAP Data Declarations | Dec-2008
ABAP Data Declarations
IBM Global Business Services
© IBM Corporation 20132 Dec-2008ABAP Data Declarations |
Objectives
The participants will be able to : List ABAP elementary data types
Declaring variables
List user-defined data types
Use arithmetic expressions
List field-symbols
IBM Global Business Services
© IBM Corporation 20133 Dec-2008ABAP Data Declarations |
C: Character Text
I: Integer
P: Packed #
F: Floating Point #
N: Numeric Text
D: Date
T: Time
X: Hexadecimal #
ABAP Elementary Data Types
STRING XSTRING
IBM Global Business Services
© IBM Corporation 20134 Dec-2008ABAP Data Declarations |
ABAP Elementary Data Types (Contd.)
C: Character Text
I: Integer
P: Packed #
F: Floating Point #
N: Numeric Text
D: Date
T: Time
X: Hexadecimal #
STRING XSTRING
IBM Global Business Services
© IBM Corporation 20135 Dec-2008ABAP Data Declarations |
DATA: PLAYER(35) TYPE C,NICKNAME(35),POINTS TYPE I,GAMES TYPE I VALUE ‘10’,AVERAGE(5) TYPE P,STARTER,ACQUIRED TYPE D.
Declaring Variables
IBM Global Business Services
© IBM Corporation 20136 Dec-2008ABAP Data Declarations |
C: (blank)
I: zero
P: zero
F: zeroes
N: zeroes
D: 00000000
T: 000000
X: 00
The “CLEAR” statement sets a field back to its initial value, not its default value.
Initial Values
IBM Global Business Services
© IBM Corporation 20137 Dec-2008ABAP Data Declarations |
DATA: PLAYER(35) TYPE C,NICKNAME(35) VALUE ‘Dr. J’,POINTS TYPE I VALUE ‘255’,GAMES TYPE I VALUE 10,AVERAGE(5) TYPE P VALUE ‘25.5’,STARTER VALUE ‘Yes’,ACQUIRED TYPE D VALUE ‘19760610’.
Assigning Default Values
IBM Global Business Services
© IBM Corporation 20138 Dec-2008ABAP Data Declarations |
DATA: PLAYER(35) TYPE C VALUE ‘Julius Erving’,NICKNAME(35),ACQUIRED TYPE D.
DATA: PLAYER(35) TYPE C VALUE ‘Julius Erving’,NICKNAME LIKE PLAYER,ACQUIRED LIKE SY-DATUM.
Use the “LIKE” addition to declare fields with the same format (i.e., data type and length)
Declaring “Like” Fields
IBM Global Business Services
© IBM Corporation 20139 Dec-2008ABAP Data Declarations |
CONSTANTS: TEAM1(20) TYPE C VALUE ‘76ers’,TEAM2 TYPE TEAM1 VALUE ‘Celtics’,TOT_GAMES TYPE I VALUE 82.
If you attempt to change the value of a constant, a syntax error will occur.
Declaring Constants
The “VALUE” addition is required.
IBM Global Business Services
© IBM Corporation 201310 Dec-2008ABAP Data Declarations |
TYPES: NAME(35) TYPE C,TEAMS(20) TYPE C.
DATA: PLAYER TYPE NAME VALUE ‘Troy Aikman’,NICKNAME TYPE PLAYER.
CONSTANTS: TEAM1 TYPE TEAMS VALUE ‘Cowboys’,TEAM2 TYPE TEAM1 VALUE ‘Packers’.
A user-defined data type created with the “TYPES” statement is used to specify a field’s data type in the “TYPE”
addition of the “DATA” or “CONSTANTS” statements.
User-Defined Data Types
IBM Global Business Services
© IBM Corporation 201311 Dec-2008ABAP Data Declarations |
Standard Length
C = defined length
I = 12
P = (2 * defined length) + 1
F = 22
N = defined length
D = 10
T = 8
X = (2 * defined length)
Justification
C = left-justified
I = right-justified
P = right-justified
F = right-justified
N = left-justified
D = left-justified
T = left-justified
X = left-justified
Output Characteristic for Data Types
IBM Global Business Services
© IBM Corporation 201312 Dec-2008ABAP Data Declarations |
DATA: FLOAT TYPE F VALUE ‘98.7654321E2’,PACK TYPE P VALUE 12,INT TYPE I VALUE 32.
WRITE: / FLOAT,/ FLOAT EXPONENT 1 DECIMALS 3,/ FLOAT EXPONENT 0 DECIMALS 2,/ PACK,/ PACK DECIMALS 1,/ INT DECIMALS 2.
9.876543210000000E+03987.654E+01
9876.5412
12.0
32.00
Output for Numeric Fields
These fields are not aligned because of the different
standard output lengths of the numeric type fields.
IBM Global Business Services
© IBM Corporation 201313 Dec-2008ABAP Data Declarations |
Demonstration
Declaring variables and constants.
IBM Global Business Services
© IBM Corporation 201314 Dec-2008ABAP Data Declarations |
Practice
Declaring variables and constants.
IBM Global Business Services
© IBM Corporation 201315 Dec-2008ABAP Data Declarations |
DATA: TITLE(25),SALARY TYPE P,CNVSALARY TYPE SALARY,
MOVE ‘President’ TO TITLE.COMPUTE SALARY = 5000000.CNVSALARY = SALARY * 3.ADD 1000 TO SALARY.
MOVE <value> TO <field>.
[COMPUTE] <field> = <value or expression>.
ADD <value> TO <field>.SUBTRACT <value> FROM <field>.MULTIPLY <field> BY <value>.DIVIDE <field> BY <value>.
Assigning Values to Fields
IBM Global Business Services
© IBM Corporation 201316 Dec-2008ABAP Data Declarations |
COUNTER = COUNTER + 1.
SALARY = BASE * BONUS_PERCENT.
LENGTH = STRLEN( NAME ).
ANSWER = ( 10 + SQRT( NUM1 ) ) / ( NUM2 - 10 ).
Spacing is very important when using arithmetic expressions!!!
FunctionsSQRT, EXP, LOG,
SIN, COS, STRLEN, . . .
Operators+ - * / **
DIV and MOD
Arithmetic Expressions
IBM Global Business Services
© IBM Corporation 201317 Dec-2008ABAP Data Declarations |
DATA: CUSTOMER(10) TYPE C,INV_DATE TYPE SYDATUM.
CUSTOMER = ‘1234567890’.INV_DATE = ‘19960626’.
WRITE: / CUSTOMER+8(2), ‘xx’,INV_DATE(4).
* Start of MonthINV_DATE+6(2) = ‘01’.CUSTOMER+6(4) = ‘ABCD’.WRITE: / CUSTOMER, ‘------’, INV_DATE.
90 xx 1996123456ABCD ----- 06/01/1996
Sub-Fields in ABAP
Use an offset and length to display or change portions of a
field.
IBM Global Business Services
© IBM Corporation 201318 Dec-2008ABAP Data Declarations |
DATA: DAYSOLD TYPE P,DOB TYPE D,TODAY TYPE SYDATUM.
DOB = ‘19621230’.TODAY = SY-DATUM.
DAYSOLD = TODAY - DOB.
WRITE: ‘You are’, DAYSOLD, ‘days old.’
You are 12410 days old.
Date Calculations in ABAP
Date fields store values as “YYYYMMDD”.
IBM Global Business Services
© IBM Corporation 201319 Dec-2008ABAP Data Declarations |
Demonstration
Assigning values to the fields and doing calculations.
IBM Global Business Services
© IBM Corporation 201320 Dec-2008ABAP Data Declarations |
Practice
Assigning values to the fields and doing calculations.
IBM Global Business Services
© IBM Corporation 201321 Dec-2008ABAP Data Declarations |
PARAMETERS: NUM TYPE I,NAME(20) DEFAULT ‘AARON’.
ADD 10 TO NUM.
WRITE: / NUM, ‘----’, NAME.
“Selection Screen”
Declaring Fields with PARAMETERS
IBM Global Business Services
© IBM Corporation 201322 Dec-2008ABAP Data Declarations |
These selection texts will be used on the selection screen
instead of the parameter names.
Selection Texts
Selection Screen With the parameter description
IBM Global Business Services
© IBM Corporation 201323 Dec-2008ABAP Data Declarations |
Demonstration
Creation of a selection screen and displaying the value in the report.
IBM Global Business Services
© IBM Corporation 201324 Dec-2008ABAP Data Declarations |
Practice
Creation of a selection screen and displaying the value in the report.
IBM Global Business Services
© IBM Corporation 201325 Dec-2008ABAP Data Declarations |
DATA: NUM TYPE I VALUE 12.FIELD-SYMBOLS: <F1>,
<F2> TYPE I,<F3> TYPE NUM.
ASSIGN: NUM TO <F1>,NUM TO <F2>,NUM TO <F3>.
WRITE: / ‘Line 1:’, NUM, <F1>, <F2>, <F3>.
<F1> = 32.
WRITE: / ‘Line 2:’, NUM, <F1>, <F2>, <F3>.
Line 1: 12 12 12 12Line 2: 32 32 32 32
Working with Field Symbols in ABAP
A field symbol is a “pointer” that assumes a
field’s address, not a field’s value.
IBM Global Business Services
© IBM Corporation 201326 Dec-2008ABAP Data Declarations |
Demonstration
Working with Field Symbols.
IBM Global Business Services
© IBM Corporation 201327 Dec-2008ABAP Data Declarations |
Practice
Working with Field Symbols.
IBM Global Business Services
© IBM Corporation 201328 Dec-2008ABAP Data Declarations |
Dynamic Assignment of Partial Strings
DATA: TEXT_LINE(30) VALUE ‘ABCDEFGHIJK’.
FIELD-SYMBOLS <FSYMBOL>.ASSIGN TEXT_LINE+2(5) TO <FSYMBOL>.
* this assigns 5 characters of text_line starting at* position 3 to the field string.
WRITE: / ‘Text Line =‘, TEXT_LINE.ULINE.WRITE: / ‘Field Symbol=‘, <FSYMBOL>.ULINE.
<FSYMBOL> = ‘1234567890’.
WRITE: / ‘Field Symbol =‘, <FSYMBOL>.ULINE.WRITE: / ‘Text Line =‘, TEXT_LINE.
IBM Global Business Services
© IBM Corporation 201329 Dec-2008ABAP Data Declarations |
PARAMETERS FIELD(8) DEFAULT ‘SY-UZEIT’.FIELD-SYMBOLS <FSYMBOL>.
ASSIGN (FIELD) TO <FSYMBOL>.
IF SY-SUBRC = 0.WRITE: / ‘The contents of field’, FIELD, <FSYMBOL>.
ELSE.WRITE: / ‘Failure assigning field’, FIELD, ‘to field symbol’.
ENDIF.
The contents of field SY-UZEIT 12:32:06
Selection Screen
List
Dynamic Field Assignment
FIELD SY-UZEIT
IBM Global Business Services
© IBM Corporation 201330 Dec-2008ABAP Data Declarations |
Demonstration
Dynamic assignment with the Field Symbol.
IBM Global Business Services
© IBM Corporation 201331 Dec-2008ABAP Data Declarations |
Practice
Dynamic assignment with the Field Symbol.
IBM Global Business Services
© IBM Corporation 201332 Dec-2008ABAP Data Declarations |
Summary
“DATA” statement is used to declare a variable (or field) in an ABAP program
"LIKE" and "TYPE" statements are used to declare fields with reference to other variables or fields
“CONSTANTS” statement is used to declare a constant
Text elements can be used to maintain a program’s title and column headings, selection texts, and text symbols. These text elements can be maintained in multiple languages
A field symbol does not reserve space for the field, but points to the field
“ASSIGN” statement associates a field to a field symbol at runtime
The system carries out automatic type conversion if variables are not of the same type
ABAP allows to display and change portions of a field by specifying an offset and length
IBM Global Business Services
© IBM Corporation 201333 Dec-2008ABAP Data Declarations |
Questions
What are the different types of data types?
What is a field symbol ? What is the advantage of using it in the program?
How to create a selection screen with the selection screen text?