Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand SAP ABAP character variables and string variables

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains how to understand SAP ABAP character variables and string variables. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "SAP ABAP character variables and string variables how to understand"!

Use the ABAP strlen function to count the number of characters contained in the following four characters and string variables.

Don't slide the screen in a hurry, try to calculate it yourself first to see if there is any discrepancy with the standard answer. Maybe people think these small knowledge points are useless, but Jerry will soon share a customer incident that I actually handled. It is precisely because this seemingly small knowledge point is not noticed that it finally affects the progress of the project.

The correct answers, in turn, are:

2

1

19

17

Explain them one by one.

strlen( lv_s ) = 2

The value of an integer variable, integer 1, is assigned to the string variable lv_s where an implicit type conversion takes place.

SAP help documents state that integer variables assigned to string variables end with "-" if the integer is negative, and end with blank characters if the integer is positive. In other words, when an implicit type conversion from an integer variable to a string variable occurs, there is an extra bit at the end of the string variable that represents the sign bit of the integer from which the assignment originated.

The extra whitespace in lv_s is clearly visible in the debugger, and 2000 is the hexadecimal encoding of whitespace. The debugger also shows that the number of lv_s strings is 2.

strlen( lv_s2 ) = 1

Compared with the previous example, the copy operation of lv_s2 has no implicit type conversion, but is directly assigned a character constant, so the number of characters is 1.

strlen( lv_ss) = 19

lv_ss is of type SSTRING, which is actually a CHAR20:

In the debugger, lv_ss consists of 18 leading blank characters, the character "1" and one trailing blank character, for a total of 20 characters. The Technical Type in the debugger is shown as C(20).

Why is strlen(lv_ss) equal to 19 instead of 20? SAP help documentation gives the answer--SSTRING is CHAR20, a variable of fixed length type. When using strlen to count strings for such variables, trailing whitespace characters should not be counted, so decrement them by one.

strlen( lv_s3) = 17

With the foundation of Example 3, this is easy. The variable lv_s3 is of type CHAR18 and is a fixed-length variable, so strlen calculates the number of strings as 18 - 1 = 17.

In the first example, we assign an integer directly to a string variable, and an implicit type conversion occurs. In real projects, this implicit type conversion is easy to see in function or ABAP-like method parameter passing. For formal arguments to functions or ABAP-like methods, implicit type conversions occur if the actual argument type we pass does not match its type, and this automatic conversion is sometimes unexpected or even easy to ignore.

Look at a real example. I once worked as Dev Angel for a Russian SAP CRM customer project and received a performance-related incident where the customer opened a UI extremely slowly and often timed out.

After debugging, I finally found that the culprit lies in the following code. This code initiates an RFC call from SAP CRM to SAP ERP to read data, Max Hit is set to 15, meaning that ERP is expected to return at most 15 records.

However, a total of 408093 records were returned from the ERP side. Clearly, the hardcoded Max Hit of 15 is not limiting at all.

At first I took it for granted that this was a bug in the ERP function that did not properly handle the Max Hit passed by the CRM caller. However, when I stepped inside the CRM function to look at iv_max_entries in the debugger, I was stunned:

It went from 15 to 3473457. What the hell is this number?!

Looking at the formal parameter definition of the function, iv_max_entries type is integer, and the hard-coded value '15' passed by the secondary development consultant is a character value, I suddenly realized.

How did '15' become the magic number 3473457?

Jerry doesn't explain, but instead asks you to look at the following code:

Execution, just output 3473457 this magic number. So where does line 4 31003500 come from? is actually the hexadecimal encoding of the string '15'.

That is, the secondary development consultant passes the hardcoded '15' to the function parameter IV_MAX_ENTRIES that accepts integer variables when RFC is called. The parameter type should be integer, so the hexadecimal code '31003500' of '15' is automatically converted to the corresponding integer 3473457. Obviously this is not the behavior expected by the development consultant, but because the program continues to run, the problem is temporarily masked.

After the RFC call completes, a nested LOOP follows. Even nested LOOP operations can be done quickly for inner tables containing up to 15 records, provided Max Hit works as expected. But now because Max Hit doesn't work, the number of internal table records has changed from 15 at most to more than 400,000 at once. The performance of nested LOOP operation on such a large internal table can be imagined.

At this point, I believe that everyone has a deeper understanding of "SAP ABAP character variables and string variables," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report