ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → Native SQL
Native SQL - Mapping of ABAP Types
In Native SQL, data can be transferred from the database to ABAP data objects in reads, and the other way round in the case of writes.
- In ADBC, ABAP data objects are bound using data references.
- ABAP data objects are specified as host variables after
EXEC SQL
.
In assignments between ABAP data objects and fields in database tables, the ABAP data objects are bound to the database fields using the Native SQL interface. ABAP data objects should usually only be bound to suitable database fields. The following tables show which elementary ABAP types match which HANA types, using the SAP HANA database as an example. There are similar assignments for other database systems.
Other versions:
7.31 | 7.40 | 7.54
Numeric Types
ABAP Type | HANA Type |
---|---|
b |
SMALLINT |
s |
SMALLINT |
i |
INTEGER |
int8 |
BIGINT |
p , length leng with dec decimal places |
DECIMAL, length 2len-1 with dec decimal places |
decfloat16 |
SMALLDECIMAL, length 16; VARBINARY, length 8 |
decfloat34 |
DECIMAL, length 34; VARBINARY, length 16 |
f |
DOUBLE |
Character-Like Types
ABAP Type | HANA Type |
---|---|
c , length len |
NVARCHAR, length len |
string |
NCLOB |
n , length len |
NVARCHAR, length len |
Byte-Like Types
ABAP Type | HANA Type |
---|---|
x , length len |
VARBINARY, length len |
xstring |
BLOB |
Date Types, Time Types, and Time Stamp Types
ABAP Type | HANA Type |
---|---|
d |
DATE; NVARCHAR, length 8 |
t |
TIME; NVARCHAR, length 6 |
utclong |
TIMESTAMP |
If the data types match, content can be passed unchanged without being checked. In all other cases, conversions are necessary. These conversions and the necessary checks are executed by the Native SQL interface. The conversions take place in the platform-dependent part (Client Library) of the Native SQL interface, and the following problems can occur:
- Unexpected conversion results
- Cut off or pad values for character-like and byte-like types
- Conversion rules different to those in ABAP
- SQL errors that raise exceptions
For this reason, conversions in the Native SQL interface should be avoided if possible. This is particularly
relevant for the ABAP types n
, d
, and t
and decimal floating point numbers.
Note
Unlike Native SQL, ABAP SQL respects the column types of database tables and views defined in the ABAP Dictionary. These are based on the built-in data types of the ABAP Dictionary, which can all be mapped to a built-in ABAP type. Therefore, the ABAP SQL interface can also execute platform-independent conversions between incompatible data types if they correspond to the conversion rules in ABAP. For example, see the assignment rules for reads.
Example
The example shows that if the data of a column with type NUMC is assigned in Native SQL to a host variable with the ABAP type n
that is too short, it is truncated on the right, but the
rule that is applied in ABAP SQL passes the data right-justified and truncated on the left.
DELETE FROM demo_expressions.
INSERT demo_expressions FROM @( VALUE #( id = 'X' numc1 = '123' ) ).
DATA host TYPE n LENGTH 5.
SELECT SINGLE numc1 FROM demo_expressions INTO (@host).
cl_demo_output=>write( |ABAP SQL: { host }| ).
EXEC SQL.
SELECT numc1 FROM demo_expressions INTO :host
ENDEXEC.
cl_demo_output=>display( |Native SQL: { host }| ).
The result in an SAP HANA database is:
Native SQL 00000
Example
The example shows that if a host variable with ABAP type n
that is too long is assigned to a column with type NUMC in Native SQL, an exception occurs. The
conversion rule that is applied in ABAP SQL, however, passes the data right-justified and truncated on the left.
DELETE FROM demo_expressions.
INSERT demo_expressions FROM @( VALUE #( id = 'X' ) ).
DATA host TYPE n LENGTH 15 VALUE '111112222233333'.
UPDATE demo_expressions SET numc1 = @host WHERE id = 'X'.
SELECT SINGLE numc1 FROM demo_expressions INTO @DATA(result).
cl_demo_output=>write( |ABAP SQL: \n{ result }| ).
TRY.
EXEC SQL.
UPDATE demo_expressions SET numc1 = :host WHERE id = 'X'
ENDEXEC.
CATCH cx_sy_native_sql_error INTO DATA(exc).
cl_demo_output=>write( |Native SQL: \n{ exc->get_text( ) }| ).
ENDTRY.
cl_demo_output=>display( ).
The result in an SAP HANA database is:
2222233333
Native SQL
An SQL error has occurred: inserted value too large for column