180 likes | 285 Vues
Learn about ABAP fields, variables, data types, constants, and more in this comprehensive guide. Understand how to declare and use fields effectively in ABAP programming.
E N D
Fields (Introduction) • In ABAP, fields (or data objects) are named locations in memory • Variables store data that might change • Constants store data that does not change • As before, fields are declared with the DATA keyword • KEEP IN MIND THAT ABAP IS A ‘BUSINESS’ PROGRAMMING LANGUAGE
Field Naming • Field names must begin with a letter or underscore character • Subsequent characters can be digits • 30 character maximum variable length • Avoid special characters • Don’t use anything but an underscore • Don’t use a dash for reasons you will discover shortly • Cannot be reserved words
Fields (Characteristics) • They nave a name • A defined length • And a data type • And possibly a length • And possibly an initial value
Data Types and the ABAP Dictionary • ABAP data types and ABAP dictionary data types are not exactly the same • Dictionary types map to more simple ABAP types • See handout
Numeric Data Types (1) • Integer numbers are whole number having a value range between -2**31 to 2**31-1 • Results are rounded not truncated • Floating point numbers (data type F) are similar to IEEE floating point numbers • They are converted to a binary value and are subject to rounding error
Numeric Data Types (2) • Packed numbers (data type P) use an internal SAP format • The size is program defined between 1 and 16 bits • Each digit occupies 4 bits • So two decimal digits are packed into one byte • Up to 14 digits allowed after the decimal point • Make sure that fixed-point arithmetic is set. otherwise values are treated as integers • P fields are slow
Declaring Variables (1) • The DATA statement declares a variable • Syntax: • DATA VARNAME TYPE DATATYPE [VALUE INITIALVALUE]. • Example: • Declare an integer having an initial value of 5. DATA IntValue1 TYPE I VALUE 5.
Packed Numbers (Example) DATA packed16 TYPE P LENGTH 7 DECIMALS 2.packed16 = '12121219.12'.WRITE packed16. • Produces
LIKE • Declare a variable having the dame data type as another variable • Makes it easier to change data types DATA demo16instance1 LIKE packed16.DATA demo16instance2 LIKE packed16.
Arithmetic • Operators are as usual +, -, /, * • Use DIV for integer division and MOD for integer remainder • THERE MUST BE A BLANK CHARACTER BEFORE AND AFTER THE ‘=‘ SIGN AND BETWEEN ARITHMETIC OPERATORS AND PARENTHESIS
Arithmetic (Type Conversion) • Looks like any other arithmetic expression (plus the space requirements) • SAP calls this compatible and convertible data types • Compatible types have the same data type, field type, … • Comparable types are converted using conversion rules (See handout)
Type Coercion (1) • The following produces “1” because the result is rounded to an int
Type Coercion (2) • Floating point data (F, P) are rounded and converted to integers • Floating point numbers are rounded when converted to packed values • When converting character fields to numbers, the source field must contain a valid number • More about dates later
System Fields • System fields are constants defined by the system • SY-SUBRC – return code of a procedure • SY-UNAME – logon name of the user • SY-DATUM – current date • SY-UZEIT – current type • SY-TCODE – current transaction code
Constants • The CONSTANTS statement declares a constant CONSTANTS pi TYPE P LENGTH 15 DECIMALS 14 VALUE '3.14159'.