EXEC SQL - CONNECT

So that you can work with native SQL statements, a connection to a database system must be defined. When you start an ABAP system, a standard link from the database interface to the central database of the SAP system is started. This connection will be defined as the current connection for native SQL statements and as a standard connection for Open-SQL statements when an ABAP program is started. Using the following SAP-specific native SQL statements, additional connections to other database systems can be started. These can then be accessed in native SQL.

Starting the Connection

Syntax

EXEC SQL.
  CONNECT TO dbs [AS con]
ENDEXEC.

Effect

This native SQL statement starts a connection to the database system dbs and makes this the current connection - that is, all the following native SQL statements work with the database system named in dbs. If a connection to the specified database system already exists, then this is used; otherwise, a new connection is set up.

For dbs, you can specify a literal or a host variable that contains a name from the column CON_NAME in the database table DBCON. The database system listed there must be supported by SAP and the technical data for the connection must be maintained. The column DBMS in the database table DBCON contains an abbreviation for the type of database system.

Using the AS addition, a name con can be assigned to the connection. For con, a literal or a character-type host variable can be specified. Its content is used as a name. The connection can then be selected using this name in the native SQL statement SET CONNECTION.

Choosing the Connection

Syntax

EXEC SQL.
  SET CONNECTION {con|DEFAULT}
ENDEXEC.

Effect

This native SQL statement sets the current connection for all following native SQL statements. For con, you can specify a literal or a character-type host variable that must contain the name of a connection already started. The name of the connection can be specified either as the database system from the database table DBCON, as given in the native SQL statement CONNECT TO, or as the name assigned there using the AS addition. With DEFAULT, the standard connection to the central database system of the current SAP system is set.

Determining the Connection

Syntax

EXEC SQL.
  GET CONNECTION :con
ENDEXEC.

Effect

This native SQL statement assigns the name of the current connection to con. For con, a character-type host variable must be specified. If the connection has been set up using the native SQL statement CONNECT TO and a name was given to it using AS, then this is assigned. If the connection was set up without using a name assignment, the name of the database system from the database table DBCON is used. If the current connection is the standard connection to the central database of the ABAP-based SAP system, con is assigned the value "DEFAULT".

Closing the Connection

Syntax

EXEC SQL.
  DISCONNECT con
ENDEXEC.

Effect

This native SQL statement closes the connection con. If con is not the current connection, this is not affected. If con is the current connection, the standard connection to the central database of the ABAP-based SAP system is simultaneously set as the current connection for all following native SQL statements.

For con, either a literal or a character-type host variable can be specified that must contain the name of a connection already opened. If a name was assigned while starting the connection using the native SQL statement CONNECT TO with the addition AS, then this name must be used; otherwise, the name of the database system from the DBCON database table must be used. The standard connection "DEFAULT" cannot be closed.

Example

Starting a connection to an Oracle database and importing all entries of a column in the database table SCARR.

PARAMETERS dbs TYPE dbcon-con_name.

DATA carrid_wa TYPE scarr-carrid.

DATA dbtype TYPE dbcon_dbms.

SELECT SINGLE dbms
       FROM dbcon
       INTO dbtype
       WHERE con_name = dbs.

IF dbtype = 'ORA'.
  TRY.
      EXEC SQL.
        CONNECT TO :dbs
      ENDEXEC.
      IF sy-subrc <> 0.
        RAISE EXCEPTION TYPE cx_sy_native_sql_error.
      ENDIF.
      EXEC SQL.
        OPEN dbcur FOR
          SELECT carrid
                 FROM scarr
      ENDEXEC.
      DO.
        EXEC SQL.
          FETCH NEXT dbcur INTO :carrid_wa
        ENDEXEC.
        IF sy-subrc <> 0.
          EXIT.
        ELSE.
          WRITE / carrid_wa.
        ENDIF.
      ENDDO.
      EXEC SQL.
        CLOSE dbcur
      ENDEXEC.
      EXEC SQL.
        DISCONNECT :dbs
      ENDEXEC.
    CATCH cx_sy_native_sql_error.
      MESSAGE `Error in Native SQL.` TYPE 'I'.
  ENDTRY.
ENDIF.