ABAPEXEC_CONNECTION - EXEC CONNECTION

Fill RESBD Structure from EBP Component Structure   SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3up  
This documentation is copyright by SAP AG.

EXEC SQL - CONNECT

So that you can work with Native SQL statements, a connection to a database system must be defined. When you start anASABAP, a standard link from the database interface to the central database of the AS ABAP is started. This connection will be defined as the current connection for Native SQL statements and as a standard connection forOpenSQL 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.

Possible AS ABAP connections to database systems are stored in theDBCON database table. You can create and change entries in the DBCON database table by using theDBA Cockpit tool.

Note

The implicit database commit that results at the change of work processes is carried out on all open connections.

Starting the Connection

Syntax

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

Effect

This NativeSQL statement starts a connection to the database system dbs and makes this the currentconnection - that is, all the following Native SQL statements work with the database system named indbs. 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 tableDBCON. The database system listed there mustbe 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, aliteral 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-like host variable that must contain the name of a connectionalready started. The name of the connection can be specified either as the database system from thedatabase table DBCON, as given in the Native SQL statement CONNECT TO, or as the name assignedthere using the AS addition. With DEFAULT, the standard connection to the central database system of the current AS ABAP 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-like host variable must be specified. If the connection has been set up using the NativeSQL statement CONNECT TO and a name was given to it using AS, then this is assigned. Ifthe 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 theAS ABAP, 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 AS ABAP 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 containthe name of a connection already opened. If a name was assigned while starting the connection usingthe 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
                 WHERE mandt = :sy-mandt
      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.


BAL Application Log Documentation   Fill RESBD Structure from EBP Component Structure  
This documentation is copyright by SAP AG.


Length: 8327 Date: 20120522 Time: 053147     triton ( 341 ms )