You are on page 1of 43
fo ABAP workshop 2010 Data & Declarative Statements Validity and Visibility * There are three contexts in which data type and data objects can be declared — Locally in Procedures + Exists while a procedure is being executed — As Components of Classes + Static components exists for the internal session of the ABAP program + Instance attributes exists for the lifetime of the object (from object instantiation until object deletion) — Globally to the Framework Program + Exists during the lifetime of the program Date Type and Data Objects Date types are templates for creation of data objects Data object is an instance of a data type and occupies as much memory as its type specifies [exception: the length of text and byte string varies according to their content] An ABAP program only works with the data this is available as the content of the data object Data objects are either created implicitly — a8 named data objects or — as anonymous data objects using CREATE DATA command - as literals Date Types * Type of Data Types — Predefined Data Types + Include predefined ABAP Types (b, c, d, f, i, n, p, string, etc), generic ABAP Types (any, any table, c, clike, csequence, data, n, numeric, object, etc) and Predefined ABAP Dictionary Types (CHAR, DATS, DEC, INT4, CURR, CLNT, etc.) — these were covered in an earlier workshop — User Defined Data Types + All ABAP programs can define their own data types. Within a program, procedures can also define local types + You declare local data types in a program either by referring to an existing data type or constructing a new type * You can declare program local data types in ABAP programs that can be used for typing or declaring additional data types and data objects according to their validity and visibility User-Defined Data Types User Defined Data Types can be created using — Apredefined ABAP type to which you refer using the TYPE addition — An existing local type in the program to which you refer using the TYPE addition — The data type of a local object in the program to which you refer using the LIKE addition — A data type in the ABAP Dictionary to which you refer using the TYPE addition. To ensure compati te with earlier releases, it is still possible to use the LIKI addition to refer to database tables and flat structures in the ABAP Dictionary. However, you should use the TYPE addition in new programs. User-Defined Data Types * Elementary Data Types * Complex Data Types « Reference Data Types Elementary Type Definition Syntax for any data type: nF ES dtype[(ien)] TYPE existing_type | LIKE data_object [DECIMALS dec] ees atype TYPE existing_type | LIKE data_object [LENGTH Jen] [DECIMALS dec] — existing_type is + one of the predefined ABAP types c, d, f, i, n, p, t, x, string, or xstring * an existing elementary local type in the program or * adata element defined in the ABAP Dictionary — data_object can be an existing data object with an elementary data type When you refer to a data element from the ABAP Dictionary, the system converts it into an elementary ABAP type. If no type and length is specified, ‘the type defaults to type character c of length 1 Elementary Type Definition (examples) TYPES mynumber TYPE i. TYPES mydistance TYPE p DECIMALS 2. TYPES mycode(3) TYPE c. TYPES text10 TYPE c LENGTH 10. TYPES text20 TYPE c LENGTH 20. TYPES myresult TYPE p LENGTH 8 DECIMAL 2. TYPES mycompany TYPE spfli-carrid. + DATA counts TYPE i. TYPES: company TYPE mycompany, myname TYPE text20, no_flights LIKE counts. LINE OF addition TYPES dtype TYPE [LINE OF] existing_type | LIKE [LINE OF] data_object — The optional LINE OF addition can be used if existing_type is a table type or if data_object is an internal table. If this addition is used dtype inherits the Properties of the line type of the internal table Examples TYPES event TYPE LINE OF event_table. TYPES wa_type LIKE LINE OF TABLE123. Complex Type Definition Structures are complex types Syntax TYPES BEGIN OF struc_type. “TYPES | INCLUDE ... TYPES END OF struc_type. The TYPES statement within the statements with BEGIN OF and END OF define components of the structure struc_type. If a component is a structured type or a new structure is defined within a structure using BEGIN OF and END OF, this results in substructures or nested structure The statement INCLUDE defines components of the structured type strue_type by Copying the components of another structured type or an existing structure at the same level. Complex Type Definition (costiues) TYPES: BEGIN OF address, name TYPE surname, street(30) TYPE c, Nested structures city TYPE spfli_type-cityfrom, END OF address, town TYPE address-city. Accessing TYPES BEGIN OF struct1 Level 2 colt TYPE i, components BEGIN OF struct2, colt TYPE i, | col2 TYPE i, | END OF struct2, | TYPES END OF struct1. | TYPES mytype TYPE struct1-struct2-col2. i Complex Type Definition (continues) REPORT demo_structure. TYPES: BEGIN OF myname, firstname TYPE c LENGTH 10, lastname TYPE c LENGTH 10, END OF myname. _ Nested Structures TYPES: BEGIN OF mylist, — client PE myname, number TYPE‘, END OF mylist DATA list TYPE mylist. list-client-firstname = ‘John’. list-client-Jastname = ‘Doe’, Different Levels list-number = 1. ae WRITE list-client-firstname. <= WRITE list-client-lastname. WRITE / ‘Number’, WRITE list-number. Copying Structure Components INCLUDE copies the components of an existing structure within another structure’s definition INCLUDE { {TYPE struc_type} | (STRUCTURE struc} } [AS name [RENAMING WITH SUFFIX suffix] Use [AS name] to alias and if multiple levels are okay Use SUFFIX if including the same structure more than once and need to be on the same LEVEL Using Include to Copy Structure Type REPORT demo_structure_with_include. TYPES: BEGIN OF myname, firstnamet ig TYPE c, lastname(10) TYPE ¢, END OF myname. TYPES: BEGIN OF myaddress, streel(20) TYPE city(18) TYPE ¢, state(2) TYPE c, Zip (5, n, END OF myaddress. TYPES: BEGIN OF myindex, number TYPE, END OF myindex. TYPES BEGIN OF mylist. INCLUDE TYPE myname AS s1. INCLUDE TYPE myaddress AS s2. INCLUDE TYPE myindex AS s3. TYPES END OF mylist. DATA mailinglist TYPE mylist. mailinglist-firstname Mailinglist-lastname = ‘Do mailinglist-street = '123 Some Street’. mailinglist-city = 'Bay City’. mailinglist-state mailinglist-zip = mailinglist-numbe WRITE / mailing WRITE mailinglis WRITE / mailinglis WRITE / mailinglist-city. WRITE mailinglist-state. WRITE mailinglist-zip. WRITE / ‘Number’. WRITE mailinglist-number. INCLUDE enables us to access all components at one level. Note: mailinglist-firsiname is same as mailinglist-s1-firsiname, etc | 14 Using Include to Copy Structure Type Multiple Times TYPES BEGIN OF mylist, LUDE TYPE myname AS si INCLUDE TYPE myedcress ‘AS S2A RENAMING WITH SUFFIX _home. INCLUDE TYPE myaddress AS s2B RENAMING WITH SUFFIX office. INCLUDE TYPE myindex AS s3. TYPES END OF mylist DATA mailinglist TYPE mylist. Same INCLUDEd structure different PP SUFFIX mailinglist-firsiname mailinglist! mailinglist-street_hom mailinglist-city_home mailinglist- mailinglist-zip_hom. fa mailinglist-street_offic mailinglist-city_oFfi mailinglist- mailinglist Internal Tables Internal tables provide a means of taking data from a fixed structure and storing it in working memory in ABAP The data is stored line by line in memory, and each line has the same structure In ABAP, internal tables fulfill the function of arrays Since they are dynamic data objects (i-e., data type defines all properties statically with the exception of memory consumption), they save us the task of dynamic memory management in our programs Use internal tables whenever you want to process a dataset with a fixed structure within a program and dynamic # of rows A particularly enporen use for internal tables is for storing, processing and formatting data from a database table within a program They are also a good way of including very complicated data ‘structures in an ABAP program Table Types - Data Type of Internal Tables Similar to Structures, Table Types represent complex ABAP data types The data objects of table types are Internal Tables The data type of an internal table is fully specified by its line type, key, and table type Syntax: + TYPES dtype TYPE|LIKE tabkind OF linetype [WITH key]... — This defines an internal table type with access type tabkind, line type linetype and key key. The line type linetype can be any known data type. Specifying the key is optional. Internal tables can thus be generic Line Type of Internal Tables * The line type of an internal table can be any data type * The data type of an internal table is normally a structure *« Each component of the structure is a column in the internal table « However, the line type may also be elementary or another internal table Key of Internal Tables . [UNIQUE | NON UNIQUE] { {KEY comp1 comp2 comps ...} | {DEFAULT KEY}}... Use unique or non unique to specify whether the table key is unique or not (key identifies table rows, internal tables with a unique key cannot contain duplicate entries) You can only use non unique key for standard tables, unique key for hashed tables and both types of keys for sorted tables Key of Internal Tables (continues; + Key types — STANDARD KEY (DEFAULT KEY) — USER-DEFINED KEY * The are two kinds of keys are the standard key [DEFAULT KEY] and a user-defined key [specified individual components comp1 comp2 etc]. For tables with structured row type, the standard key is formed from all character-type columns [not numeric i, p, f nor table types] of the internal table. 20 Key of Internal Tables (continues; * Ifa table has an elementary line type [i.e., non structured], the default key is the entire line (row). If the row type is also a table, an empty key is defined * The user-defined key can contain any columns of the internal table that are not internal table themselves, and do not contain internal tables. References are allowed as table keys. Internal tables with a user-defined key are called key tables + For the generic table types ANY TABLE or INDEX TABLE you can only specify a key without specifying the uniqueness a Row Type of Internal Table Non generic data type from ABAP dictionary Non generic program local data type Any ABAP type — generic (c, d, f, i, n, p, etc) or — non generic (any, c, clike, numeric, etc) Using REF TO makes the row type a reference type Instead of type an data object dobj from the program specified. By doing so the type of the object is adopted for the row type Table Types of Internal Tables 7 4 [STANDARD] TABLE} | SORTED TABLE | HASHED TABLE | ANY ABLE | INDEX TABLE)} . The table type determines how ABAP will access individual table entries ‘Standard tables are managed by a logical index. The system can access records either by using the table index or the key. The key of a standard table is always non-unique. You cannot specify a unique key. Sorted tables are managed by a logical index (similar to standard tables). The entries are listed in ascending order according to table key Hashed tables are managed by hash algorithm. There is no logical index. The entries are not ordered in the memory. The position of a row is calculated by specifying a key using a hash function Index tables include both the standard tables and sorted tables To find out the access type of an internal table at runtime, use the statement DESCRIBE TABLE KINI 23 Internal Table (exampie) TYPES: BEGIN OF portfolio_type, name(25) TYPE c, socialsecunity(®) TYPE 6 assets TYPE p LENGTH 8 DECIMALS 2, END OF portfolio_type. TYPES mytab TYPE STANDARD TABLE OF portfolic DATA wa TYPE portfolio_type. DATA itab TYPE mytab. e wa-name = ‘John Doe’. List_Eat_goo Sam Heo wa-socialsecurity = "123456789". wa-assets = '123456.50'. APPEND wa TO itab. type WITH DEFAULT KI wa-name = ‘Harry Smith’, wa-socialsecurity = "567565678". John Bee user 1a wa-assets = 5000.50". ieee Shey bats APPEND wa TO itab, LOOP AT itab INTO wa. WRITE: / wa-name, wa-socialsecurity, wa-assets. ENDLOOP. 24 Internal Table (continued) * The optional addition WITH HEADER LINE declares an extra data object with the same name and line type as the internal table. This data object is known as the header line of the internal table. You use it as a work area when working with the internal table * But, the WITH HEADER LINE addition is obsolete; you should no longer use it. Internal Table (continued) . INITIAL SIZE n... Specify a number of rows n as a numeric literal or numeric constant, to adjust the first block of memory reserved by system for the internal table Without specifying this value or specifying 0, the system allocates an appropriate initial memory size When required the next additional memory block twice the initial size is reserved, as long as this size does not exceed 8 KB Additional memory blocks are created with a constant size of 12 KB each 26 Reference Data Types TYPES dtype { {TYPE REF TO type } | {LIKE REF TO dobj} } Use TYPE addition to define data types for data and object reference variables Use LIKE addition to define data type for data reference variables There are two types of references that are possible, DATA Reference and OBJECT Reference DATA Reference ¢ Ifthe specified data type data is predefined generic data type, the system creates a data type for a data reference variable from the static type data. Such reference variables can refer to any data object but can only be dereference in the statement ASSIGN * Ifthe specified data type data is any non-generic data type from ABAP dictionary, locally defined type or a non generic predefined type, the system creates a data type for a data reference variable with the relevant static type. Such reference variables can refer to all data objects of the same type and can be dereferenced to matching operand positions using the dereferencing operator ->* 28 DATA Reference (continuea) * By specifying a data object for dobj, the system creates a data type for a data reference variable whose static type is adopted from the data type of the data object. Such reference variables can refer to all data objects of the dame type and can be dereferenced at matching operand positions using the dereferencing ->*. Within a procedure, you cannot specify a generic typed formal parameter for dobj Object Reference * By specifying a global or a local class for type, the system creates a data type for a class reference whose static type is the specified class. Such reference variables can refer to all instances of the class and its subclass TYPE-POOLS Type pools are the precursors to general type definitions in the ABAP Dictionary. Before release 4.0, only elementary data types and flat structures could be defined in the ABAP Dictionary. All other types that should've been generally available had to be defined with TYPES in type pools. As of release 4.0, type pools were only necessary for constants. As of release 6.40, constants can be declared in the public sections of global classes and type pools can be replaced by global classes. In other words TYPE-POOLS can be considered obsolete. 3a Literals * These are defined in the source code of a program and are fully determined by their value Literals may be of type — Numeric Literals — Text Field Literals — String Literals . Numeric Literals Numeric Literals consists of continuous sequence of numbers preceded by a sign Numeric Literals between -2147483648 and 214748648 have a build-in ABAP type i (4 byte integer) Numeric Literals outside this range and up to 15 digits have build-in ABAP type p (packed) with length of 8 bytes p(8) Numeric Literals having more than 15 digits have build-in ABAP type p (packed) with length of 16 bytes p(16) Numeric Literals (coninues) ‘When passing a numeric literal to a formal parameter of a function, the check made are based on + fall numeric literals are allowed i,b, s > all numeric literals are allowed * 11> the value of the numeric literals must not be negative and the number of digits are smaller or equal to the length of the formal parameter + p> if the formal parameter is generic, its length is set to 16 and number of decimals to 0. If the program attribute ‘Fixed point arithmetic’ is not set, the formal parameter must not have any decimal-places or the literal must have the value zero Text Field Literals Text field Literals are character strings included in single inverted commas (‘) They have data type of c There are no empty text field literals; “ is same as text field literal ‘‘ of length 1 The length lie between 1 and 255 characters To represent an inverted comma you must enter 2 consecutive inverted commas Text Symbols can be used by appending its three-digit identifier ### where applicable Text Field Literals (continues) * What is a Text Symbols? A text symbol is a named data object that is generated when you start the program from the (predefined) texts in the text pool of the ABAP program ... ‘Literal (###) or ‘Literal'(123)... > ### or 123 or ABC is text symbol defined below and is used in place of the ‘Literal’ if the is (text symbol) is pre- defined Note: there is no space between the Literal and the text symbol Note2: to directly access the text symbol use (text-+HH#) i. = text-123. "ABAP Edito sepa Help ect a String Literals * String Literals are character strings included in single back quotes (~) and have the data type of string * Empty string literal ** represents string of length zero * To represent a back quote within a string, enter two consecutive back quotes * Asstring literal can be up to 255 characters * There are no literals for byte fields/strings a7 Create Data All of the data objects that you define in the declaration part of a program using statements such as DATA are created statically, and already exist when you start the program. To create a data object dynamically during a program, you need a data reference variable and the following statement: CREATE DATA dref {TYPE type}|{LIKE dobj}. This statement creates a data object in the internal session of the current ABAP program. After the statement, the data reference in the data reference variable dref points to the object. The data object that you create does not have its own name. You can only address it using a data reference variable. To access the contents of the data object, you must dereference the data reference. FIELD-SYMBOLS FIELD-SYMBOL typing | structure * Field symbols are placeholders or symbolic names for other fields, They do not physically reserve space for a field, but point to its contents. A field symbol can pat to any data object. The data ‘object to which a field symbol points, is assigned to it after it has been declared in the program. + Whenever you address a field symbol in a program. you are seoreesn the field that is assigned to the field symbol. After successful assignment, there is no difference in ABAP whether you reference the field symbol or the field itself. You must assign a field to each field symbol before you can address the latter in programs 39 FIELD-SYMBOLS + All operations programmed with field symbols are applied to the field assigned to it. For example, a MOVE statement between two field symbols moves the contents of the field assigned to the first field symbol to the field assigned ta the second field symbol. The field symbols themselves point to the same fields after the MOVE statement as they did before + You can create field symbols either without or with type specifications. If you do not specify a ‘ype, the field symbol inherits all of the technical attributes of the field assigned to it. If you do specify a type, the system checks the compatibility of the field symbol and the field you are assigning to it during the ASSIGN statement 40 ASSIGN Statement ¢ Ifyou already know the name of the field that you want to assign to the field symbol when you write a program, use the static ASSIGN statement: * ASSIGN TO . + When you assign the data object, the system checks whether the technical attributes of the data object correspond to any type specifications for the field symbol . The field symbol adopts any generic attributes of that are not contained in its own type specification. After the assignment, it points to in memory 44 Field Symbol and ASSIGN REPORT demo_field_syrmbols_stat_assign FIELD-SYMBOLS: TYPE ANY, ASSIGN num TO , DESCRIBE FIELD LENGTH IN CHARACTER MODE. WRITE: J <1, has length’, num. Static ASSIGN ASSIGN line2-col2 TO , feve ie eSie ae eevrncrce