Skip to content

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.

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.

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.