ABAP Geek 15 – ADBC
ADBC is the abbreviation for “ABAP Database Connectivity” that is a class-based API for the Native SQL interface of the Application Server ABAP. ADBC allows object orientated and dynamic access to the Native SQL interface and can always be used when access to a database via the Native instead of the Open SQL interface is necessary and static EXEC SQL is not sufficient (remember that dynamic token specification is not possible in EXEC SQL).
How to find the classes of ADBC?
The ADBC classes all begin with the prefix CL_SQL_ or CX_SQL_. The most important classes are
- CL_SQL_STATEMENT for executing SQL statements
- CL_SQL_PREPARED_STATEMENT for creating prepared SQL statements
- CL_SQL_CONNECTION for administering database connections
- CX_SQL_EXCEPTION as an exception class
How to use ADBC?
In the most simple case you use instance methods of CL_SQL_STATEMENT to exceute SQL statements that are passed to the methods. You can create DML and DDL statements, carry out queries and execute stored procedures.
A simple example
PARAMETERS key TYPE scarr-carrid.
DATA: sql TYPE REF TO cl_sql_statement,
result TYPE REF TO cl_sql_result_set,
exc TYPE REF TO cx_sql_exception,
stmnt TYPE string,
carrid TYPE scarr-carrid,
carrname TYPE scarr-carrname,
dref TYPE REF TO data,
rc TYPE i.
TRY.
CREATE OBJECT sql.
CONCATENATE `SELECT carrid, carrname `
`FROM scarr `
`WHERE carrid = ‘` key `’` INTO stmnt.
result = sql->execute_query( stmnt ).
GET REFERENCE OF carrid INTO dref.
result->set_param( dref ).
GET REFERENCE OF carrname INTO dref.
result->set_param( dref ).
rc = result->next( ).
IF rc > 0.
WRITE: / carrid, carrname.
ELSE.
WRITE / ‘No entry found’.
ENDIF.
result->close( ).
CATCH cx_sql_exception INTO exc.
MESSAGE exc TYPE ‘I’ DISPLAY LIKE ‘E’.
ENDTRY.
A SELECT statement is passed to an object of CL_SQL_STATEMENT and the method EXECUTE_QUERY is invoked. With SET_PARAM the columns of the result set are accessed in order to bind data refeferences to the individual results.
More Information
http://help.sap.com/abapdocu_70/en/ABENADBC.htm
http://www.sappro.com/article.cfm?id=3375
(maybe also in SDN but I didn’t find it)
Thanks for this blog.
Graham Robbo
This is a Very useful blog for all categories of developers.
Thanks,
Venkat
I want to use table parameter for MS SQL server 2008 but I don't know if it's working or not.
Do you have any idea?
Thanks.
Regards,
Sue
I'm sorry, but I don't understand your question.
(what do you mean with 'table parameter for MS SQL server 2008'?)
Kind regards
Horst
CREATE OBJECT blob_ref
exporting con_ref = lo_connection
.
lo_statement->SET_PARAM_BLOB(
EXPORTING
BLOB_REF = blob_ref ).
blob_ref->put_bytes(
EXPORTING
XSTR = lw_data-file_content ).