Application Development and Automation 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: 
Read only

constants use

Former Member
0 Likes
2,722

Hi Freinds,

why we are using constants in prg...

pls.. explain any one with EXAMPLE...

REGARDS,

5 REPLIES 5
Read only

former_member194669
Active Contributor
0 Likes
1,156

Hi,

Constants are data objects that you can create statically in the program using that statement


constants : c_x type c value 'X'.

It cannot subsequently be changed. The value of a constant cannot be changed during the execution of the program.


constants : c_x type c value 'X'.
if v_value eq c_x.
   " Here you are check v_value = 'X' using constant c_x.
endif.

a®

Read only

Former Member
0 Likes
1,156

Hi,

If the client requirement is fixed for a particular thing..you use Constants...

For eg U have a report to list materials for specific plant lets say 'C'

U can give this plant 'C' as constant in ur where condition .

select matnr from MARA into table itab where werks = c_plant .

Where c_plant = 'C' .

praveen

Read only

Former Member
0 Likes
1,156

<b>Constants </b>

Constants are named data objects that you create statically using a declarative statement. They allow you to store data under a particular name within the memory area of a program. The value of a constant must be defined when you declare it. It cannot subsequently be changed. The value of a constant cannot be changed during the execution of the program. If you try to change the value of a constant, a syntax error or runtime error occurs.

CONSTANTS: BEGIN OF myaddress,

name TYPE c LENGTH 20 VALUE 'Fred Flintstone',

street TYPE c LENGTH 20 VALUE 'Cave Avenue',

number TYPE p VALUE 11,

postcode TYPE n LENGTH 5 VALUE 98765,

city TYPE c LENGTH 20 VALUE 'Bedrock',

END OF myaddress.

Read only

Former Member
0 Likes
1,156

Hi venu,

You can use a constant when you need to include the same literal many times in a program. You can define a constant with the same value as the literal and use the constant in the body of the program in place of the literal. After some time, if you want to change the values means, you can simply change the value where you define the constant.

ABAP/4 has one pre-defined constant: SPACE ( You can use ' ' this also both are same ).

It is a constant having a value equal to spaces.

Defining constants:

--> CONSTANTS C_EX(4) TYPE C VALUE 'VENU'.

--> CONSTANTS C_EX LIKE C VALUE 'BB'.

--> CONSTANTS C_NUM TYPE I VALUE 5.

--> CONSTANTS C_DATE LIKE SY-DATUM VALUE '19970305'.

Read only

Former Member
0 Likes
1,156

Variants:

1. CONSTANTS c. ... VALUE [ val | IS INITIAL ].

2. CONSTANTS c(len) ... VALUE [ val | IS INITIAL ].

3. CONSTANTS: BEGIN OF crec,

...

END OF crec.

Effect

The CONSTANTS statement defines global and local constants. By using constants, you can read statically declared data objects, since they always have a particular data type. You can use constants in any position where fields are allowed, and also in the following positions:

To specify length in a DATA STATICS, CONSTANTS, PARAMETERS, or TYPES statement.

As a DECIMALS value in a DATA, STATICS, CONSTANTS, PARAMETERS, or TYPES statement.

As an OCCURS value in a DATA, STATICS, RANGES, or TYPES statement.

In the VALUE addition of a DATA, STATICS, or CONSTANTS statement.

As an offset or length specification in a field specification.

As a value for an exception (EXCEPTIONS) in the CALL FUNCTION statement.

Constants always have a defined data type. Data types and data objects are integral parts of the ABAP type concept.

In contrast to variables defined with the DATA statement, you cannot change the value of a constant once it has been defined.

Apart from the additions ... TYPE typ OCCURS n, ... LIKE f1 OCCURS n and WITH HEADER LINE, all the additions used with the DATA statement are allowed. However, in contrast to the DATA statement, the addition ... VALUE val or VALUE IS INITIAL is obligatory with variants 1 and 2. See additions with DATA.

Example

CONSTANTS CHAR1 VALUE 'X'.

CONSTANTS INT TYPE I VALUE 99.

CONSTANTS: BEGIN OF CONST_REC,

C(2) TYPE I VALUE 'XX',

N(2) TYPE N VALUE '12',

X TYPE X VALUE 'FF',

I TYPE I VALUE 99,

P TYPE P VALUE 99,

F TYPE F VALUE '9.99E9',

D TYPE D VALUE '19950101',

T TYPE T VALUE '235959',

END OF CONST_REC.

Note

You cannot declare XSTRINGs, references and internal tables - or structures that include internal tables - as constants.