SAPSQL_DATA_LOSS ABAP Programming Error
At runtime I experienced SAPSQL_DATA_LOSS ABAP programming error in SAP after I change Select statement with SPRAS language criteria to new Open SQL syntax ABAP for HANA. I was just checking my previous codes developed in classic ABAP Select statements and re-writing some by transforming the old syntax to new enhanced ABAP Open SQL syntax which was introduced with NetWeaver AS ABAP 7.4
ABAP Programming Error
SAPSQL_DATA_LOSS
Data was lost while copying a value.
Since the error was not caught by the ABAP compiler in ABAP Editor and at my first meet I could not find any information about SAPSQL_DATA_LOSS ABAP programming error on the web, I decided to take some notes on my web blog kodyaz.com
Below sample codes are showing the previous and updated versions of the ABAP Select statement which the change caused the ABAP dumps at runtime that can be detailed by ST22 transaction code.
As programmers will see, it seems a standart way of transforming the ABAP Select for new syntax which introduced inline variable declarations and some performance improvements. ABAP developers which are new to this syntax can remember that the columns are seperated by comma and parameters are identified by "@" in front of them.
Unfortunately, with some improper parameter declaration for the form routine code including this select statement and the PERFORM statement calling this routine; ABAP programming error SAPSQL_DATA_LOSS was triggered.
When I execute SAP transaction ST22 to see details of the error I could reach below description which is actually enough to solve the issue.
ABAP Programming Error
SAPSQL_DATA_LOSS
Data was lost while copying a value.
The current ABAP program was trying to execute an Open SQL statement and discovered that a value needs to be copied into the database field type. This copy operation resulted in data loss. Either the data could not be converted, the data in source was not long enough to fill the target, or the source was a non-character type structure. The value was "TR". The ABAP field has type "C" length 1 with 0 decimal places. The target has type "C" length 1 with 0 decimal places.
If you are an ABAP developer, you know the language field has a conversion behind like EN to E for English, TR to T for Turkish, etc. The Langu or Spras is in fact is defined as a single character in ABAP data dictionary.
On the other hand, the previously used classic ABAP syntax is flexable enough to convert the two character inputs to expected data type for the language fields. So the prior code block for ABAP Select works fine.
On the contrary, the second enhanced ABAP Open SQL syntax demands the exact data type. And if it not supplied as in this case causes a programming error resulting with ABAP dump.
ABAP SAPSQL_DATA_LOSS error is similar to SQL Server data truncate errors in a way.
Solution for the ABAP error SAPSQL_DATA_LOSS in my case is to strictly define USING parameter for language. This will enable the ABAP editor to identify possible errors beforehand.