SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow

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.

" prior ABAP code
DATA lv_maktx TYPE maktx.
SELECT SINGLE maktx INTO lv_maktx
 FROM makt
 WHERE spras EQ piv_lang
  AND matnr EQ piv_matnr.

" new enhanced OSQL syntax, ABAP for HANA
SELECT SINGLE maktx INTO @DATA(lv_maktx)
 FROM makt
 WHERE spras EQ @piv_lang
  AND matnr EQ @piv_matnr.
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.

DATA ls_vbpr TYPE vbrp.
ls_vbpr-matnr = '000000000000140586'.
DATA lv_maktx LIKE makt-maktx.

* Erroneous call method with language or spras USING parameter with TR value
*PERFORM get_material_desc USING ls_vbpr-matnr 'TR' CHANGING lv_maktx.
PERFORM get_material_desc USING ls_vbpr-matnr 'T' CHANGING lv_maktx.

* In below Form definition missing data types in USING parameter for language was causing error
*FORM get_material_desc USING piv_matnr TYPE matnr piv_lang CHANGING pcv_maktx.
FORM get_material_desc USING piv_matnr TYPE matnr piv_lang TYPE langu CHANGING pcv_maktx.

SELECT SINGLE maktx INTO @DATA(lv_maktx)
 FROM makt
 WHERE spras EQ @piv_lang
 AND matnr EQ @piv_matnr.

IF sy-subrc IS INITIAL.
 pcv_maktx = lv_maktx.
ENDIF.

ENDFORM.
Code


SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.