Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP - Database Accesses →  ADBC - ABAP Database Connectivity →  Examples of ADBC 

ADBC, Parameter Binding

The example demonstrates the binding of parameters in ADBC.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_adbc_ddl_dml_binding.

PARAMETERS  p_name TYPE c LENGTH 10 DEFAULT 'mytab'.
SELECTION-SCREEN SKIP.
PARAMETERS: p_create RADIOBUTTON GROUP grp,
            p_insert RADIOBUTTON GROUP grp,
            p_select RADIOBUTTON GROUP grp,
            p_drop   RADIOBUTTON GROUP grp.
SELECTION-SCREEN SKIP.
PARAMETERS  p_key TYPE i DEFAULT 1.

CLASS adbc DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
  PRIVATE SECTION.
    CLASS-DATA: dbname TYPE string,
                sql TYPE REF TO cl_sql_statement,
                wa1 TYPE c LENGTH 10,
                wa2 TYPE c LENGTH 10,
                err TYPE REF TO cx_sql_exception .
    CLASS-METHODS: create RAISING cx_sql_exception,
                   insert RAISING cx_sql_exception,
                   select RAISING cx_sql_exception,
                   drop   RAISING cx_sql_exception.
ENDCLASS.

CLASS adbc IMPLEMENTATION.
  METHOD main.
    dbname = 'ABAP_DOCU_DEMO_' && p_name.
    TRY.
        CREATE OBJECT sql.
        IF p_create = 'X'.
          create( ).
          MESSAGE 'Create was successful' TYPE 'S'.
        ELSEIF p_insert = 'X'.
          insert( ).
          MESSAGE 'Insert was successful' TYPE 'S'.
        ELSEIF p_select = 'X'.
          select( ).
          MESSAGE 'Select was successful' TYPE 'S'.
        ELSEIF p_drop   = 'X'.
          drop( ).
          MESSAGE 'Drop was successful' TYPE 'S'.
        ENDIF.
      CATCH cx_sql_exception INTO err.
        MESSAGE err TYPE 'I' DISPLAY LIKE 'E'.
    ENDTRY.
  ENDMETHOD.
  METHOD create.
    sql->execute_ddl(
      `CREATE TABLE ` && dbname   &&
      `( val1 char(10) NOT NULL,` &&
      `  val2 char(10) NOT NULL,` &&
      `  PRIMARY KEY (val1) )` ).
  ENDMETHOD.
  METHOD insert.
    DATA dref TYPE REF TO data.
    DO 100 TIMES.
      GET REFERENCE OF wa1 INTO dref.
      sql->set_param( dref ).
      GET REFERENCE OF wa2 INTO dref.
      sql->set_param( dref ).
      wa1 = sy-index.
      wa2 = sy-index ** 2.
      sql->execute_update(
       `INSERT INTO ` && dbname && ` VALUES (?,?)` ).
    ENDDO.
  ENDMETHOD.
  METHOD select.
    DATA: result TYPE REF TO cl_sql_result_set,
          msg    TYPE c LENGTH 30,
          key    TYPE c LENGTH 10,
          dref   TYPE REF TO data,
          rc     TYPE i.
    key = p_key.
    GET REFERENCE OF key INTO dref.
    sql->set_param( dref ).
    result = sql->execute_query(
      `SELECT val1, val2 ` &&
      `FROM ` && dbname && ` ` &&
      `WHERE val1 = ?` ).
    GET REFERENCE OF wa1 INTO dref.
    result->set_param( dref ).
    GET REFERENCE OF wa2 INTO dref.
    result->set_param( dref ).
    rc = result->next( ).
    IF rc > 0.
      WRITE: 'Result:' TO msg,
             wa1 TO msg+10,
             wa2 TO msg+20.
    ELSE.
      msg = 'No entry found'.
    ENDIF.
    result->close( ).
    MESSAGE msg TYPE 'I'.
  ENDMETHOD.
  METHOD drop.
    sql->execute_ddl(
     `DROP TABLE ` && dbname ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  adbc=>main( ).

Description

The program is identical to DDL and DML, for example, except that here the arguments of the INSERT and SELECT statements are specified in the form of ? parameters that are bound to ABAP data objects.