When working with SAP ABAP CDS views, you'll often encounter scenarios where you need to manipulate string data using built-in functions like SUBSTRING
, LEFT
, or RIGHT
. However, things get complicated when you're dealing with RAWSTRING type fields, also represented as abap.string(0)
in certain standard SAP tables.
The challenge? Standard CDS string functions simply don't work with RAWSTRING data types. If you attempt to apply any string function to a RAWSTRING field, you'll encounter errors like:
Function LEFT: parameter at position 1 has incorrect data type STRG
The Data Type Casting Limitation
Before diving into the solution, let's understand why this happens. In SAP ABAP CDS views, data type conversion follows strict rules. According to SAP's data type casting matrix , RAW fields cannot be cast to any other data type. This fundamental limitation blocks us from using standard string manipulation functions directly.
The casting restriction means we need an alternative approach - one that operates at the database level where we have more flexibility with string operations.
The Solution: CDS Table Function with AMDP
The most effective solution is to create a CDS Table Function implemented with an AMDP (ABAP Managed Database Procedure) method. This approach leverages SQL Script's native string functions, which can handle raw string manipulation that standard CDS functions cannot.
Step 1: Create the CDS Table Function
First, let's create the table function definition:
@EndUserText.label: 'Table function for CDATAF'
define table function ZT_AIF_CDATAF
returns
{
key MANDT : abap.clnt;
key MSGGUID : sxmsguid;
key SAVE_COUNTER : int4;
key FIELD_COUNTER : int4;
FIELD_NAME : abap.char(100);
value_new : /aif/value;
value_old : /aif/value;
}
implemented by method
zcl_aif_cdataf=>get_data;
Step 2: Implement the AMDP Class
Next, we create the AMDP class that contains the actual truncation logic:
CLASS zcl_aif_cdataf DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb .
CLASS-METHODS: get_data FOR TABLE FUNCTION zt_aif_cdataf.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
Step 3: The Core Implementation
Here's where the magic happens - the AMDP method implementation:
CLASS zcl_aif_cdataf IMPLEMENTATION.
METHOD get_data BY DATABASE FUNCTION
FOR HDB LANGUAGE SQLSCRIPT
USING /aif/cdataf.
declare lv_str "$ABAP.type( CHAR200 )";
declare lv_locate "$ABAP.type( CHAR10 )";
declare lv_indx int := 0;
declare lv_result int := 0;
-- Fetch data from db table
lt_data = select mandt, msgguid, save_counter, field_counter, substr_after (field_path, '-' ) as field_name,
long_value_new as value_new, long_value_old as value_old from "/AIF/CDATAF";
-- Process each record
for lv_indx in 1..record_count( :lt_data )
DO
-- Hold field path in a variable and the location of first hyphen( - )
lv_str = :lt_data.field_name[ lv_indx ];
lv_locate = LOCATE (lv_str, '-');
-- Keep spliting the string till you find last field
WHILE lv_locate != 0
DO
lv_str = substr_after (lv_str, '-' );
lv_locate = LOCATE (lv_str, '-');
END while;
-- Assign back the truncated string with final field name to itab
lt_data.field_name[ lv_indx ] = lv_str;
END for;
-- Return modified result
RETURN SELECT mandt, msgguid, save_counter, field_counter, field_name, value_new, value_old FROM :lt_data;
ENDMETHOD.
ENDCLASS.
Understanding the Logic
The implementation processes data from the /AIF/CDATAF
table, which contains field paths in this format:
APPLICATION_ENGINE-IDOC-INBOUND-FAILED-MATNR
Our logic extracts the last substring after the final hyphen, giving us the actual field name (MATNR
in this example). The SQL Script environment allows us to use string functions like SUBSTR_AFTER
and LOCATE
on raw string data, which wouldn't be possible in standard CDS views.
Using the Table Function in Other CDS Views
Once created, you can easily consume this table function's FIELD_NAME
field in any other CDS view through associations or joins. Since both your main CDS view and this table function likely share the MSGGUID
field, you can link them using a LEFT OUTER JOIN:
define view Z_MY_ENHANCED_VIEW
as select from MY_BASE_CDS as base
left outer join ZT_AIF_CDATAF as tf
on base.MSGGUID = tf.MSGGUID
on base.SAVE_COUNTER = tf.SAVE_COUNTER
on FIELD_COUNTER = tf.FIELD_COUNTER
{
key base.MSGGUID,
base.SAVE_COUNTER,
base FIELD_COUNTER,
base.other_fields,
tf.FIELD_NAME -- The truncated field from table function
}
This approach allows you to seamlessly integrate the processed field data into any existing CDS view structure.
Alternative Approaches for Raw String Manipulation
While the table function approach is the most versatile, there are other methods depending on your specific use case and system requirements:
1. Custom CDS View Entity with Implementation Class
Create a CDS view entity and implement the truncation logic in ABAP within the implementation class. This method works well for OData services and SAP Fiori applications but has limited scope.
2. Virtual Elements with Calculated Fields
Define a CDS view with virtual elements using the @ObjectModel.virtualElementCalculatedBy
annotation. Write the truncation logic in ABAP within the specified class. Like the previous approach, this is primarily suitable for OData services.
3. Analytics Cube with Read Class
For analytical scenarios, use @Analytics.dataCategory: #CUBE
with @Analytics.readClassName
annotation. This approach works with both OData services and SAC queries, making it suitable for reporting use cases.
4. Table Function with AMDP (Recommended)
As demonstrated in this post, this approach offers the most flexibility. It works seamlessly with OData services, can be consumed in SAC queries, and provides the best performance for complex string operations.
Why Choose Table Functions?
Among all available options, table functions offer several key advantages:
-
Database-level processing: Operations execute directly on the database, ensuring optimal performance
-
Broad compatibility: Works with OData services, SAC queries, and standard CDS consumption
-
SQL Script flexibility: Access to comprehensive string manipulation functions unavailable in standard CDS
-
Reusability: Once created, can be consumed by multiple CDS views across your landscape
Conclusion
Dealing with RAWSTRING data in CDS views requires a strategic approach since standard string functions are incompatible with this data type. The CDS Table Function with AMDP implementation provides an elegant, performant solution that overcomes these limitations while maintaining broad compatibility across different SAP technologies.
This approach not only solves the immediate technical challenge but also creates a reusable component that can be leveraged throughout your development landscape. Whether you're building analytical reports, OData services, or complex data transformations, this pattern will serve as a reliable foundation for raw string manipulation in your CDS architecture.
For more SAP ABAP and CDS insights, check out my other technical posts on SAP development and follow my journey on LinkedIn