Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  Native SQL →  ADBC - ABAP Database Connectivity →  ADBC Examples 

ADBC, Prepared Statement

The example demonstrates how a prepared statement is created and executed using ADBC.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    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 ).
          result_tab = VALUE #( BASE result_tab
                               FOR j = 1 WHILE result->next( ) > 0
                                ( result_line ) ).
        ENDLOOP.
        sql->close( ).
        cl_demo_output=>display( result_tab ).
      CATCH cx_sql_exception INTO DATA(err).
        cl_demo_output=>display( err->get_text( ) ).
    ENDTRY.

Description

In this example, a query is instantiated as a prepared statement and is executed with various parameters.

The work process cannot switch between the calls of the prepared statement, which means that dialogs using dynpros are not possible in the corresponding loop. Instead, the parameters to be evaluated are prepared and collected in a single internal table and the results lists are prepared and collected in another internal table.