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: 

ERROR: The "LENGTH" declaration is only valid for types C, N, X, or P.

kk_india_2023
Explorer
0 Kudos
  1. DATA Ch1(20) VALUE 'KIRAN KUMAR'.
  2. WRITE: / 'Ch1 = ', Ch1.
  3. DATA Ch2 LENGTH 20 VALUE 'KIRAN KUMAR' .
  4. WRITE: / 'Ch2 = ', Ch2.

I am getting error at line number 4.

ERROR: The "LENGTH" declaration is only valid for types C, N, X, or P.

My Question is: By default an Object 'Ch2' is of type C, then why I am getting this error ?

Thank You.

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor
0 Kudos

As ABAP documentation - DATA, TYPE abap_type says:

Syntax

DATA { {var[(len)] TYPE abap_type [DECIMALS dec]}
     | {var [TYPE abap_type [LENGTH len] [DECIMALS dec]]} }
     [VALUE val|{IS INITIAL}]
     [READ-ONLY].
  • "For reasons of legibility, it is best to include all information and always use the addition LENGTH instead of parentheses to specify the length len."

And if you indicate LENGTH, as you can see above, you must also indicate TYPE.

And it's the answer to your question: By default an Object 'Ch2' is of type C, then why I am getting this error ?

9 REPLIES 9

nomssi
Active Contributor

you are missing a TYPE specification, i.e. DATA ch2 TYPE c LENGTH 20.

0 Kudos

I have missed TYPE specification in Line Number 1 and I am not getting any error at Line Number 1,

but I am getting error at Line Number 4.

What is the underlying reason ?

Thank You.

shantraj
Explorer
0 Kudos

You are missing the data type here, that is C.

DATA Ch2 type c LENGTH 20 VALUE 'KIRAN KUMAR'.

However, below syntax reads it as type as C, N, X or P. depending upon the value we pass on to it.

That's why it is not throwing any error.

DATA Ch1(20) VALUE 'KIRAN KUMAR'.

0 Kudos

I do not agree with your answer which says:

"However, below syntax reads it as type as C, N, X or P. depending upon the value we pass on to it."

Because in ABAP, the default data type is Character type.

In the variable declaration statement, if the data type of a variable is not defined then the compiler will assign the data type of a variable as Character Type. I have tested the code by setting break point.

In the below statement:

DATA Ch1(20) VALUE 'KIRAN KUMAR'.

During compile time the compiler will assign data type of a variable Ch1 as Character type.

If my explanation or understanding is not correct please correct me with valid reference or docs.

Thank You.

former_member184158
Active Contributor
0 Kudos

Hi,

you have to write the type ( C, D , I) etc.

so in your case you have to write the type C length 20

DATA Ch1(20) VALUE 'KIRAN KUMAR'.
WRITE: / 'Ch1 = ', Ch1.

DATA Ch2 Type C LENGTH 20 VALUE 'KIRAN KUMAR' .
WRITE: / 'Ch2 = ', Ch2.
" you can use the Inline Declaration
DATA(ch3) = 'KIRAN KUMAR'.
WRITE: / 'Ch3 = ', Ch3.

Sandra_Rossi
Active Contributor
0 Kudos

As ABAP documentation - DATA, TYPE abap_type says:

Syntax

DATA { {var[(len)] TYPE abap_type [DECIMALS dec]}
     | {var [TYPE abap_type [LENGTH len] [DECIMALS dec]]} }
     [VALUE val|{IS INITIAL}]
     [READ-ONLY].
  • "For reasons of legibility, it is best to include all information and always use the addition LENGTH instead of parentheses to specify the length len."

And if you indicate LENGTH, as you can see above, you must also indicate TYPE.

And it's the answer to your question: By default an Object 'Ch2' is of type C, then why I am getting this error ?

0 Kudos

My doubt is clear with your specific answer.

Thank You.

raymond_giuseppi
Active Contributor

Read carefully the documentation on DATA syntax. The LENGTH option requires to use a TYPE

DATA { {var[(len)] TYPE abap_type [DECIMALS dec]}
     | {var [TYPE abap_type [LENGTH len] [DECIMALS dec]]} }
     [VALUE val|{IS INITIAL}]
     [READ-ONLY].

0 Kudos

My doubt is clear with your explanation.

Thank you.