Field Symbols

August 26, 2017 | Author: Manish Shankar | Category: Data Type, Pointer (Computer Programming), C (Programming Language), Notation, Computer Engineering
Share Embed Donate


Short Description

Field Symbols...

Description

SAP Field Symbols

© Manish Shankar

Field Symbols: Field symbols allow to access data objects dynamically in ABAP programs. Unlike static access to a data object, where you need to specify the name of the object, field symbols allow you to access and pass data objects whose name and attributes you do not know until runtime. When we address a field symbol, the system works with the contents of the data object assigned to it, and not with the contents of the field symbol itself. Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for a field, but point to its contents. The data object to which a field symbols points is assigned to it after it has been declared in the program. Whenever we address a field symbol in a program, you are addressing 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. Field symbols are similar to dereferenced pointers in C (that is, pointers to which the content operator * is applied). However, the only real equivalent of pointers in ABAP, that is, variables that contain a memory address (reference) and that can be used without the contents operator, are reference variables in ABAP Objects. Field symbols can be created either without or with type specifications. If type is not specified, the field symbol inherits all of the technical attributes of the field assigned to it. If we specify a type, the system checks the compatibility of the field symbol and the field you are assigning to it during the ASSIGN statement. Field symbols provide greater flexibility when we address data objects: • • • •

If we want to process sections of fields, we can specify the offset and length of the field dynamically. You we assign one field symbol to another, which allows us to address parts of fields. Assignments to field symbols may extend beyond field boundaries. This allows you to address regular sequences of fields in memory efficiently. We can also force a field symbol to take different technical attributes from those of the field assigned to it.

The flexibility of field symbols provides elegant solutions to certain problems. On the other hand, it does mean that errors can easily occur. Since fields are not assigned to field symbols until runtime, the effectiveness of syntax and security checks is very limited for operations involving field symbols. This can lead to runtime errors or incorrect data assignments. While runtime errors indicate an obvious problem, incorrect data assignments are dangerous because they can be very difficult to detect. For this reason, we should only use field symbols if we cannot achieve the same result using other ABAP statements. © Manish Shankar

Declaring Field Symbols: Statement to declare a field symbol FIELD-SYMBOLS [|STRUCTURE DEFAULT ].

Example: REPORT

ProgramName .

field-symbols type any. Data : empname(20) type c , empid(6) type n . assign empname to . = 'Raghawan'. assign empid to . = '123456'. write : / empname, / empid .

Output:

For field symbols, the angle brackets are part of the syntax. They identify field symbols in the program code. If you do not specify any additions, the field symbol can have data objects of any type assigned to it. When you assign a data object, the field symbol inherits its technical attributes. The data type of the assigned data object becomes the actual data type of the field symbol.

© Manish Shankar

The addition allows to specify the type of a field symbol. When we assign a data object to a field symbol, the system checks whether the type of the data object you are trying to assign is compatible with that of the field symbol. If the types are not compatible or convertible, the system reacts with a syntax or runtime error. If however, we want to assign the type of the field symbol to the data object by means of casting, we must do so explicitly using the ASSIGN statement. The system then treats the assigned data object as if it had the same type as the field symbol. After declaring a field symbol and assigning it to required field makes no difference whether we are referring the field symbol or data object. Type of the field symbol: Either we can use any elementary type or dictionary type or generic type. Generic type in the scene of type any.

Casting Data Objects: When we assign a data object to a field symbol you can cast to any data type. This means that any area of memory can be considered to have assumed a given type. Using the CASTING addition to the ASSIGN statement allows us to assign a data object to a field symbol where the type of the data object is incompatible with that of the field symbol. There are two types of CASTING: casting with an implicit type declaration and casting with an explicit type declaration. Casting with an Implicit Type Declaration: Provided the field symbol is either fully typed or has one of the generic built-in ABAP types – C, N, P, or X – we can use the following statement: ASSIGN ... TO CASTING. When the system accesses the field symbol, the content of the assigned data object is interpreted as if it had the same type as the field symbol. The length and alignment of the data object must be compatible with the field symbol type. Otherwise the system returns a runtime error. If the type of either the field symbol or the data object is – or contains – a string, reference type, or internal table, the type and position of these components must match exactly. Otherwise, a runtime error occurs. REPORT ProgramName. TYPES: BEGIN OF t_date, year(4) TYPE month(2) TYPE day(2) TYPE END OF t_date. FIELD-SYMBOLS TYPE ASSIGN sy-datum TO WRITE / sy-datum.

© Manish Shankar

n, n, n, t_date. CASTING.

SKIP. WRITE: / -year , / -month, / -day. Output:

In this example, the field symbol is fully typed using the local program type t_date . The SY-DATUM field can be treated like a structure as a result of the CASTING addition to the ASSIGN statement. This would not be possible without the CASTING addition, since SY-DATUM is incompatible with the field symbol type. Casting with an Explicit Type Declaration If the field symbol is neither fully typed nor generically typed, use the following form of the ASSIGN statement: ASSIGN ... TO CASTING TYPE |LIKE [DECIMALS ].

When the system accesses the field symbol, the content of the assigned data object is interpreted as if it had the type declared in the statement. After the TYPE addition, we can declare the name of a data object enclosed in parentheses. The content of this data object indicates the data type at runtime. Example: © Manish Shankar

REPORT ProgramName . TYPES: BEGIN OF t_date, year(4) TYPE n, month(2) TYPE n, day(2) TYPE n, END OF t_date. FIELD-SYMBOLS: TYPE ANY, TYPE n. ASSIGN sy-datum TO CASTING TYPE t_date. WRITE / sy-datum. SKIP. DO. ASSIGN COMPONENT sy-index OF STRUCTURE TO . IF sy-subrc 0. EXIT. ENDIF. WRITE / . ENDDO.

Output:

Generic Type Specification: The following types allow you more freedom when using actual parameters. The data object only needs to have the selection of attributes specified. Type specification No type specification

© Manish Shankar

Check for data object All types of data object are accepted. The field symbol

adopts all of the attributes of the data object. TYPE ANY TYPE C, N, P, or X

Only data objects with type C, N, P, or X are accepted. The field symbol adopts the field length and DECIMALS specification (type P) of the data object. TYPE TABLE The system checks whether the data object is a standard internal table. This is a shortened form of TYPE STANDARD TABLE (see below). TYPE ANY TABLE The system checks whether the data object is an internal table. The field symbol inherits all of the attributes (line type, table type, key) from the data object. TYPE INDEX TABLE The system checks whether the data object is an index table (standard or sorted table). The field symbol inherits all of the attributes (line type, table type, key) from the data object. TYPE STANDARD TABLE The system checks whether the data object is a standard internal table. The field symbol inherits all of the remaining attributes (line type, key) from the data object. TYPE SORTED TABLE The system checks whether the actual parameter is a sorted internal table. The field symbol inherits all of the remaining attributes (line type, key) from the data object. TYPE HASHED TABLE The system checks whether the actual parameter is a hashed internal table. The field symbol inherits all of the remaining attributes (line type, key) from the data object.

Assigning structures by component: Syntax: Assign component of structure struct1 to . When you have scenario where struct1 is generically define you can use this to access the component of the structure. Example: REPORT programName . DATA:wa_sflight TYPE sflight. SELECT SINGLE * FROM sflight INTO

wa_sflight.

PERFORM write_any_line USING wa_sflight. *& Form write_any_line FORM write_any_line USING line TYPE any.

© Manish Shankar

FIELD-SYMBOLS TYPE any. DO . ASSIGN COMPONENT sy-index OF STRUCTURE line TO . IF sy-subrc NE 0. EXIT. ENDIF. WRITE / . ENDDO. ENDFORM.

Output:

© Manish Shankar

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF