Vous êtes sur la page 1sur 10

String Operations and field symbols

Different types of string operations available in SAP ABAP


programming, using field symbols in ABAP programs

Field Symbols in SAP ABAP


Use of field symbols in SAP ABAP programming, using field symbols in SAP
ABAP programs to increase performance
Field symbols are similar to pointers in C language, field symbols dosen`t have any memory
instead they will be pointing to a memory location.
The concept of field symbols is very important in order to increase the performance of SAP
applications, but unethical use of field-symbols leads to application issues.
Field symbol name should always be within <>, example:<FS_MARA>.
Syntax for declaring a field symbol.
FIELD-SYMBOLS : <FIELD_SYMBOL> TYPE MARA-MATNR. "here MARA-MATNR is a variable
type
FIELD-SYMBOLS : <FIELD_SYMBOL> TYPE MARA. "here MARA is a structure
FIELD-SYMBOLS : <FIELD_SYMBOL> TYPE REF TO DATA . "here DATA is a reference type
ASSIGNING and ASSIGN are the keywords which are used to assign a value to the field symbol.

Example of using field symbol as work area


In the below example we are going to use field symbol as work area.
REPORT ZSAPN_FIELDSYMBOLS.

DATA : IT_MARA TYPE TABLE OF MARA.


DATA : WA_MARA TYPE MARA.
FIELD-SYMBOLS : <FS_MARA> TYPE MARA.
SELECT * FROM MARA
INTO TABLE IT_MARA UP TO 50 ROWS.

LOOP AT IT_MARA ASSIGNING <FS_MARA>.


IF <FS_MARA> IS ASSIGNED.
WRITE :/ <FS_MARA>-MATNR, <FS_MARA>-MTART, <FS_MARA>-MEINS.
ENDIF.
ENDLOOP.

Check weather the field symbol is assigned or not before processing field symbol, it is very
important to check field symbol, if it is not assigned it will get a run-time error, use the
blow syntax to check field symbol assignment.
IF <FS_MARA> IS ASSIGNED.
**process field symbol
ENDIF.

Increase performance using field symbols in SAP ABAP


I belive there are always performance differences between reference variables(work area) and
memory pointing(field symbols), here I wants to add some proof for that.
Below you will find two programs one with work area and another one with field symbols, execute
them and see time difference at the end of the program( will display at the bottom of output), you
will find difference.
program1
REPORT ZSAPN_FIELDSYMBOLS1.
DATA : IT_MARA TYPE TABLE OF MARA. "internal table
FIELD-SYMBOLS : <FS_MARA> TYPE MARA. "field symbols
DATA : LV_TIME_START TYPE TIMESTAMPL. "time at start of loop
DATA : LV_TIME_END TYPE TIMESTAMPL. "time at the end of loop
DATA LV_TIME_TAKEN TYPE P DECIMALS 8. "time difference

SELECT * FROM MARA "select data from MARA


INTO TABLE IT_MARA UP TO 20000 ROWS.

GET TIME STAMP FIELD LV_TIME_START. "get time at start of loop


LOOP AT IT_MARA ASSIGNING <FS_MARA>. "loop start
WRITE :/ <FS_MARA>-MATNR, <FS_MARA>-MTART, <FS_MARA>-MEINS.
ENDLOOP.

GET TIME STAMP FIELD LV_TIME_END. "get time at the end of loop
LV_TIME_TAKEN = LV_TIME_END - LV_TIME_START. "time taken
WRITE:/ 'Time taken: ', LV_TIME_TAKEN. "display time taken

Program2
REPORT ZSAPN_FIELDSYMBOLS2.
DATA : IT_MARA TYPE TABLE OF MARA. "internal table
DATA : WA_MARA TYPE MARA. "work area
DATA : LV_TIME_START TYPE TIMESTAMPL. "time at start of loop
DATA : LV_TIME_END TYPE TIMESTAMPL. "time at the end of loop
DATA LV_TIME_TAKEN TYPE P DECIMALS 8. "time difference

SELECT * FROM MARA "select data from MARA


INTO TABLE IT_MARA UP TO 20000 ROWS.

GET TIME STAMP FIELD LV_TIME_START. "get time at start of loop


LOOP AT IT_MARA INTO WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.
ENDLOOP.

GET TIME STAMP FIELD LV_TIME_END. "get time at the end of loop
LV_TIME_TAKEN = LV_TIME_END - LV_TIME_START. "time taken
WRITE:/ 'Time taken: ', LV_TIME_TAKEN. "display time taken

See the time take to process loop in both programs.

Using SPLIT in SAP ABAP


Using SPLIT function to divide a string at a value into different variables
SPLIT is a key word which is used to cut a string into pieces at a specified value.
Keyword syntax isSPLIT AT '' INTO .

Example of using SPLIT in SAP ABAP


REPORT ZSAPN_STRING.
DATA : LV_STRING TYPE STRING . "decleration for main string
DATA : LV_STRING1 TYPE STRING, "decleration for splitting string into
LV_STRING2 TYPE STRING, "decleration for splitting string into
LV_STRING3 TYPE STRING, "decleration for splitting string into
LV_STRING4 TYPE STRING. "decleration for splitting string into
LV_STRING = 'SPLIT ME AT SPACE'. "main string value
SPLIT LV_STRING AT ' ' INTO LV_STRING1 LV_STRING2 LV_STRING3 LV_STRING4. "split

the main string into specified fields at space


WRITE :/ LV_STRING1. "print splitted fields
WRITE :/ LV_STRING2.
WRITE :/ LV_STRING3.
WRITE :/ LV_STRING4.

Example of using SPLIT into internal table


Instead of splitting the data into individual fields we can split data into an internal table.
REPORT ZSAPN_STRING.

DATA : LV_STRING TYPE STRING .


TYPES: BEGIN OF TY_STRING,

STR(25) TYPE C,
END OF TY_STRING.
DATA IT_STRING TYPE TABLE OF TY_STRING.
DATA WA_STRING TYPE TY_STRING .

LV_STRING = 'SPLIT ME AT SPACE'.


SPLIT LV_STRING AT ' ' INTO TABLE IT_STRING .

LOOP AT IT_STRING INTO WA_STRING.


WRITE :/ WA_STRING-STR.
ENDLOOP.

Using CONCATENATE in SAP


ABAP
Using concatenate to combine different variables in to one
CONCATENATE is a string function which is used to add different variables into a single
string variable.

Syntax : CONCATENATE <STRING1> <STRING2> <STRING3>... INTO <STRING>.

Example of Using CONCATENATE


REPORT ZSAPN_CONCATENATE.
DATA : STR1 TYPE STRING.
DATA : STR2 TYPE STRING.
DATA : STR3 TYPE STRING.

DATA : STRING TYPE STRING.


STR1 = 'A'.
STR2 = 'B'.
STR3 = 'C'.
CONCATENATE STR1 STR2 STR3 INTO STRING. "Concatenate into string

WRITE :/ STRING .
*The out put will be 'ABC'

CONCATENATE with saperated by


REPORT ZSAPN_CONCATENATE.
DATA : STR1 TYPE STRING.
DATA : STR2 TYPE STRING.
DATA : STR3 TYPE STRING.

DATA : STRING TYPE STRING.


STR1 = 'THIS IS'.

STR2 = 'EXAMPLE OF'.


STR3 = 'CONCATENATE'.
CONCATENATE STR1 STR2 STR3 INTO STRING SEPARATED BY ' '. "Concatenate into string
separated by space

WRITE :/ STRING .
*The out put will be 'THIS IS EXAMPLE OF CONCATENATE'

Using TRANSLATE in SAP ABAP


Using TRANSLATE to translate a string into upper case from lower case and
from lower case to upper case
TRANSLATE is a string function which is used to change the case of a string i:e from lower case
to upper case and upper case to lower.
TRANSLATE <STRING> TO <LOWER CASE/UPPER CASE>.

Example of Using TRANSLATE


REPORT ZSAPN_TRANSLATE
.
DATA : LV_STRING TYPE STRING.
LV_STRING = 'small to upper case'.
TRANSLATE LV_STRING TO UPPER CASE.
WRITE : / LV_STRING.

Using CONDENSE to remove


blank spaces in a string in
ABAP
Using CONDENSE to remove blank spaces in SAP ABAP programming, string
function CONDENSE
CONDENSE is a string function/keyword which is used to remove blank spaces in a given string.
Syntax: CONDENSE <STRING>.

Example of using CONDENSE


ZSAPN_CONSENSE.
DATA LV_STRING TYPE STRING.
LV_STRING = 'THIS IS STRING WITH SPACES'.
CONDENSE LV_STRING.
WRITE : LV_STRING. "out put will be THISISSTRINGWITHSPACES

Using STRLEN to find a string


length in SAP ABAP
Using STRLEN to determine the length of a string variable in SAP ABAP
programming
The keyword/ string function STRLEN is used to determine the length of a string in SAP ABAP
programs.
Syntax: STRLEN( <STRING> ).

Using STRLEN to find a string length


REPORT ZSAPN_STRLEN.
DATA : STRING TYPE STRING.
DATA LV_LENGTH TYPE I . "variable to store length
STRING = 'THIS IS SOME STRING'.
LV_LENGTH = STRLEN( STRING ).
WRITE : LV_LENGTH. "out put will be 19

Vous aimerez peut-être aussi