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: 

ABAP CDS issue with using replace : Number of positions > maximum number (1333) for data type CHAR

Hello All,

I try to display log messages in a IDA ALV based on a CDS view.

I have a log message table which contains the the message class,

the message number and the values of the variables &1 to &4.

Afterwards I join table T100 and try to replace the variables of the message. But when using the replace function like this

replace(MessageText ,'&1', MessageVariable1) as msgstring

I get the following error message when activating the view.

"Number of positions > maximum number (1333) for data type CHAR"

Casting of the result seems to be ok but using nested replace statement, leads to the same error.

Any suggestions to solve this problem?

Regards,

Anurag.

8 REPLIES 8

horst_keller
Product and Topic Expert
Product and Topic Expert

0 Kudos

Sorry for this necro posting 🙂

But I have the same problem and definitely size of message from T100 can't be more than 1333 character, because field T100-text has size 73 characters, argument MSGV* has size CHAR50 and it is definitely less than 1333 characters and from my point of view there is no error 🙂

Could you please explain?

Thank you in advance

0 Kudos

Hi Horst, we have this same problem and it definitely doesn't work as documented.

When building a message string from a message file where you have

MESSAGE = "This message contains variable = &1" and VARIABLE1 = "My value"

You need to replace '&1' with the value from VARIABLE1

MESSAGE starts off as 70 characters long, while VARIABLE1 is 50 characters long.

The SQL statement for the REPLACE should be

REPLACE(MESSAGE, '&1', VARIABLE1)

The resulting string should be 41 characters long, since all trailing blanks should be ignored from both MESSAGE and VARIABLE1.

Instead we get the message "Number of positions > maximum number (1333) for data type CHAR".

The REPLACE function is not working correctly.

I've done similar 100s of times in Microsoft SQL and have no problem with it returning the correct length result, so something seems very wrong with SAP SQL statement for CDS.

Can you please explain your answer as to why you believe it is doing exactly as documented in this case?

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

See my answer from November 13,2023.

former_member836264
Discoverer
0 Kudos

Did you find the solution?

0 Kudos

I'm getting above error while upgrading our SAP box in CDS views. Any solution/pointers ?

In case anybody wonder how max length is calculated, when using REPLACE function, see link below:

https://eduardocopat.github.io/abap-docs/7.54/abencds_f1_sql_functions_character/

Unforutantely, i cannot find in abap docu section in below printscreen

image.png

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

The latest documentation sql_func - String Functions or CDS DDL - CDS View Entity, String Functions says for REPLACE:

Result Type: SSTRING if an argument has the type SSTRING, otherwise CHAR with the maximum possible length of the result.

The maximum possible length of the result must take into account that sql_exp2 might fill up sql_exp1. Therefore, the maximum possible length is calculated by dividing the length of sql_exp1 by the length of sql_exp2, multiplied by the length of sql_exp3.This can easily reach 1333, see first Hint in the above documentation (the "slightly" is a glitch of the translator's pen and will be replaced by "easily reach" in an upcoming version).

See

SELECT SINGLE text, 
              replace( text, '&', '1' ) as msg1, 
              replace( text, '&', '12' ) as msg2, 
              replace( text, '&', '123' ) as msg3, 
              replace( text, '&', '1234' ) as msg4
              FROM t100
              WHERE sprsl = 'E' AND
                    arbgb = 'SABAPDEMOS' AND
                    msgnr = '888'
             INTO @DATA(message).

The result length increases as follows: 73, 146, 219, 292, ...

Testing the limit:

FINAL(out) = cl_demo_output=>new( ).

DATA repl TYPE c LENGTH 100.
DATA result TYPE string.

DO.
  repl &&= 'X'.
  TRY.
      SELECT SINGLE (`REPLACE( text, '&', @repl ) as msg`)
                    FROM t100
                    WHERE sprsl = 'E' AND
                          arbgb = 'SABAPDEMOS' AND
                          msgnr = '888'
                   INTO @result.
      out->write( |{ result } ({ strlen( result ) })| ).
    CATCH cx_sy_open_sql_db INTO FINAL(exc).
      out->write( exc->get_text( ) ).
      EXIT.
  ENDTRY.
ENDDO.

out->display( ).

The limit of 1333 characters is already exceeded at the 18th iteration. Although the relevant text content has a length of only 75, ABAP SQL has to reserve 73 x 18 characters which is greater than 1333. In the preceding iterations, the reserved but unused characters are filled with blanks.

Please calculate the maximum possible length of the result in your case and check if the REPLACE function really behaves differently than documented.