Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Field symbols

Former Member
0 Kudos

How FIELD-SYMBOLS can be used and can i create an internal table using field-symbols?

Plz help me.

Thanks,

6 REPLIES 6

Former Member
0 Kudos

HI,

you can create an itab using field-symbol.

<b>example:</b>

<b>(1)</b>

REPORT ZDYN_ITAB.

PARAMETERS:DB_TABLE(30).

DATA FCAT1 TYPE LVC_T_FCAT.

DATA:DYN_ITAB TYPE REF TO DATA,

WA TYPE REF TO DATA.

FIELD-SYMBOLS: <DISP_TABLE> TYPE TABLE,

<WA> TYPE ANY.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = DB_TABLE

CHANGING

CT_FIELDCAT = FCAT1[].

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = FCAT1[]

IMPORTING

EP_TABLE = DYN_ITAB.

ASSIGN DYN_ITAB->* TO <DISP_TABLE>.

CREATE DATA WA LIKE LINE OF <DISP_TABLE>.

ASSIGN WA->* TO <WA>.

SELECT * FROM (db_table) INTO <WA>.

APPEND <WA> TO <DISP_table>.

ENDSELECT.

DESCRIBE TABLE <DISP_table>.

WRITE:/ SY-TFILL.

*********************************************************************

<b>(2)</b>

DATA: L_TABNAME TYPE DD02L-TABNAME VALUE 'MARA'.

DATA : REF_TAB TYPE REF TO DATA.

FIELD-SYMBOLS: <FTAB> TYPE TABLE.

**Create the ITAB dynamically and Assign to Field symbol

CREATE DATA REF_TAB TYPE TABLE OF (L_TABNAME).

ASSIGN REF_TAB->* TO <FTAB>.

**Construct the Select statement dynamically..

SELECT * FROM (L_TABNAME) INTO TABLE <FTAB>.

DESCRIBE TABLE <FTAB>.

WRITE:/ SY-TFILL.

rgds,

bharat.

Former Member
0 Kudos

Hi,

Check the below Links :

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 cam point 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 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. (For more information, see Data References).

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 to 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 type, 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.

Field symbols provide greater flexibility when you address data objects:

• If you want to process sections of fields, you can specify the offset and length of the field dynamically.

• You can assign one field symbol to another, which allows you 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.

• You 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, you should only use field symbols if you cannot achieve the same result using other ABAP statements.

For example, you may want to process part of a string where the offset and length depend on the contents of the field. You could use field symbols in this case. However, since the MOVE statement also supports variable offset and length specifications, you should use it instead. The MOVE statement (with your own auxiliary variables if required) is much safer than using field symbols, since it cannot address memory beyond the boundary of a field. However, field symbols may improve performance in some cases.

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3860358411d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3860358411d1829f0000e829fbfe/frameset.htm

http://www.erpgenie.com/abap/code/chap2401.txt

http://help.sap.com/saphelp_nw04/helpdata/en/16/0dce0a0cf711d3b9360000e8353423/content.htm

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3930358411d1829f0000e829fbfe/content.htm

ashish

varma_narayana
Active Contributor
0 Kudos

Hi..

This is the code for u. Try it...

DATA : REF_TAB TYPE REF TO DATA.

FIELD-SYMBOLS: <FTAB> TYPE TABLE.

PARAMETERS: P_TABLE TYPE DD02L-TABNAME VALUE 'EKPO'.

CREATE DATA REF_TAB TYPE TABLE OF (L_TABLE).

ASSIGN REF_TAB->* TO <FTAB>.

SELECT * FROM (P_TABLE) INTO TABLE <FTAB>.

DESCRIBE TABLE <FTAB>.

WRITE:/ SY-TFILL.

<b>REWARD IF HELPFUL.</b>

Former Member
0 Kudos

Hi Sagar,

Field Symbols :

1. are place holders or symbolic names for other fileds.

2. They donot physically reserve space for a field , but points to its content

3. A field symbol can point to any data object.

4. The data object to which a field symbol points is assigned to it after it has been declared in the program.

FIELD-SYMBOLS

Basic form

FIELD-SYMBOLS <fs>.

Extras:

1. ... TYPE type

2. ... TYPE REF TO cif

3. ... TYPE REF TO DATA

4. ... TYPE LINE OF type

5. ... LIKE s

6. ... LIKE LINE OF s

7. ... TYPE tabkind

8. ... STRUCTURE s DEFAULT wa

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas. See Cannot Use Untyped Field Symbols ad Cannot Use Field Symbols as Components of Classes.

Effect

This statement declares a symbolic field called <fs>. At runtime, you can assign a concrete field to the field symbol using ASSIGN. All operations performed with the field symbol then directly affect the field assigned to it.

You can only use one of the additions.

Example

Output aircraft type from the table SFLIGHT using a field symbol:

FIELD-SYMBOLS <PT> TYPE ANY.

DATA SFLIGHT_WA TYPE SFLIGHT.

...

ASSIGN SFLIGHT_WA-PLANETYPE TO <PT>.

WRITE <PT>.

Addition 1

... TYPE type

Addition 2

... TYPE REF TO cif

Addition 3

... TYPE REF TO DATA

Addition 4

... TYPE LINE OF type

Addition 5

... LIKE s

Addition 6

... LIKE LINE OF s

Addition 7

... TYPE tabkind

Effect

You can define the type of the field symbol using additions 2 to 7 (just as you can for FORM parameters (compare Defining the Type of Subroutine Parameters). When you use the ASSIGN statement, the system carries out the same type checks as for USING parameters of FORMs.

This addition is not allowed in an ABAP Objects context. See Cannot Use Obsolete Casting for FIELD SYMBOLS.

In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. See Defining Types Using STRUCTURE.

Effect

Assigns any (internal) field string or structure to the field symbol from the ABAP Dictionary (s). All fields of the structure can be addressed by name: <fs>-fieldname. The structured field symbol points initially to the work area wa specified after DEFAULT.

The work area wa must be at least as long as the structure s. If s contains fields of the type I or F, wa should have the structure s or at least begin in that way, since otherwise alignment problems may occur.

Example

Address components of the flight bookings table SBOOK using a field symbol:

DATA SBOOK_WA LIKE SBOOK.

FIELD-SYMBOLS <SB> STRUCTURE SBOOK

DEFAULT SBOOK_WA.

...

WRITE: <SB>-BOOKID, <SB>-FLDATE.

Advantages:

The following overheads can be overcome by using field symbols.

1. when you read from an internal table , there are no overheads for copying the table line to the work area.

2. when you chnage an internal table with the MODIFY stmt, you must first fill a workarea with values and then assign them to the internal table.

This can improve performance if you have large or complex internal tables.

Reward If useful.

Regards, Chitra

Former Member
0 Kudos

Hi Sagar,

FIELD-SYMBOL <fs>

The field symbols work similarly to pointers in C. They are used in conjunction with statement

ASSIGN f TO <fs> .

At runtime we can assign a field or structure to the field symbol , any changes or work can be done on the field symbol affecting directly the assigned field. The syntax of field symbol declaration is:

FIELD-SYMBOL <fs>.

Declares a field symbol <fs> which can be assigned any single field.

FIELD-SYMBOL <fs> TYPE /LIKE type.

Declares a field symbol <fs> which can be assigned any single field

which is of particular type.

FIELD-SYMBOL <fs> STRUCTURE s DEFAULT wa.

Declares a field symbol <fs> which will point to a structure(table) and the

default work area will be wa.

FIELD-SYMBOL <fs> TYPE LINE OF /LIKE LINE OF type.

Declares a field symbol <fs> which will have line type .

e.g. FIELD-SYMBOLS <fs>

TABLES kna1. * Customer master

SELECT .....

...

ASSIGN kna1-kunnr TO <fs>.

WRITE <fs>.

ASSIGN f TO <fs>

This will assign the field f to field symbol <fs>,now we can work with the field symbol instead of the field.

regards,

Ruthra