SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Accesses → Native SQL → ADBC - ABAP Database Connectivity → Examples of ADBC →ADBC, Prepared Statement
The example demonstrates the generation and execution of a prepared statement with ADBC.
Source Code
REPORT demo_adbc_prepared_statement.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
PRIVATE SECTION.
CLASS-DATA: BEGIN OF result_line,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
END OF result_line,
result_tab LIKE TABLE OF result_line.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA: sql TYPE REF TO cl_sql_prepared_statement,
cols TYPE adbc_column_tab,
carrid TYPE sflight-carrid,
carrid_tab TYPE TABLE OF sflight-carrid.
cols = VALUE #( ( CONV adbc_name( 'CARRID' ) )
( CONV adbc_name( 'CONNID' ) ) ).
carrid_tab = VALUE #( ( CONV s_carrid( 'AA' ) )
( CONV s_carrid( 'LH' ) )
( CONV s_carrid( 'UA' ) ) ).
TRY.
sql = NEW #( `SELECT carrid, connid ` &&
`FROM spfli ` &&
`WHERE mandt = '` && sy-mandt &&
`' AND carrid = ?` ).
sql->set_param( REF #( carrid ) ).
LOOP AT carrid_tab INTO carrid.
DATA(result) = sql->execute_query( ).
result->set_param_struct( struct_ref = REF #( result_line )
corresponding_fields = cols ).
WHILE result->next( ) > 0.
APPEND result_line TO result_tab.
ENDWHILE.
ENDLOOP.
sql->close( ).
cl_demo_output=>display( result_tab ).
CATCH cx_sql_exception INTO DATA(err).
cl_demo_output=>display( err->get_text( ) ).
ENDTRY.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Description
In this example, a query is instantiated as a prepared statement and is executed with various parameters.
Since the work process is not allowed to be switched between the calls of the prepared statement, no dialog using screens is possible in the corresponding loop. Instead, the parameters that are to be evaluated are prepared and collected in one internal table and the result lists are prepared and collected in another internal table.