Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP - Database Accesses →  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,
           result     TYPE REF TO cl_sql_result_set,
           err        TYPE REF TO cx_sql_exception,
           cols       TYPE adbc_column_tab,
           carrid     TYPE sflight-carrid,
           carrid_tab TYPE TABLE OF sflight-carrid,
           dref1      TYPE REF TO data,
           dref2      TYPE REF TO data.
    APPEND 'CARRID' TO cols.
    APPEND 'CONNID' TO cols.
    APPEND 'AA' TO carrid_tab.
    APPEND 'LH' TO carrid_tab.
    APPEND 'UA' TO carrid_tab.
    TRY.
        CREATE OBJECT sql
          EXPORTING
            statement = `SELECT carrid, connid `      &&
                       `FROM spfli `                 &&
                        `WHERE mandt = '` && sy-mandt &&
                        `' AND carrid = ?`.
        GET REFERENCE OF carrid       INTO dref1.
        GET REFERENCE OF result_line INTO dref2.
        sql->set_param( dref1 ).
        LOOP AT carrid_tab INTO carrid.
          result = sql->execute_query( ).
          result->set_param_struct( struct_ref = dref2
                                   corresponding_fields = cols ).
          WHILE result->next( ) > 0.
            APPEND result_line TO result_tab.
          ENDWHILE.
        ENDLOOP.
        sql->close( ).
        display( ).
      CATCH cx_sql_exception INTO err.
        MESSAGE err TYPE 'I' DISPLAY LIKE 'E'.
    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.