Ansicht
Dokumentation

ABAPEXEC_CONNECTION - EXEC CONNECTION

ABAPEXEC_CONNECTION - EXEC CONNECTION

CPI1466 during Backup   ROGBILLS - Synchronize billing plans  
This documentation is copyright by SAP AG.
SAP E-Book

- CONNECT

A connection to a database must be defined in order to be able to use Native SQL statements. When an AS ABAP is started, a standard connection from the database interface to the standard AS ABAP database is opened. This connection is defined as the current connection for embedded Native SQL statements and as a standard connection for statements when an ABAP program is started. The following SAP-specific Native SQL statements can be used to open additional database connections, which can then be accessed in static Native SQL.

The possible additional AS ABAP connections to database systems are secondary connections defined in the database table DBCON or service connections whose name starts with R/3*.

Note

For detailed information about database connections, see database connections.

Opening a Connection

EXEC SQL.
  CONNECT TO conn $[AS name$]
ENDEXEC.

Effect

This static Native SQL statement requests a database connection called conn. This connection is opened or reused if it already exists in an inactive state for the current work process. Once CONNECT TO is executed, the specified connection is the current connection of the internal session, which means that all subsequent static Native SQL statements work with this connection until a connection other than the current is set using a new CONNECT TO statement or using SET CONNECTION.

For conn, a literal or a host variable that contains one of the following values can be specified:

Both of these instances are case-sensitive. If a secondary connection that is not in the database table DBCON is specified, a catchable exception of the class CX_SY_NATIVE_SQL_ERROR is raised. If a secondary database cannot be accessed, sy-subrc is set to 4.

The addition AS can be used to specify a name name for the connection. For name, a literal or a character-like host variable can be specified whose content is used as the name. A connection called name is not the same connection as a connection requested without using the addition AS. This makes it possible to open parallel connections with separate database LUWs for the same secondary connection or service connection within an internal session. In an internal session, only one active database session can be called name. Any attempt to activate a further connection with the same name produces a runtime error. A connection called name can only be specified in the statement SET CONNECTION using this name.

Notes

Selecting a Connection

EXEC SQL.
  SET CONNECTION ${conn$|DEFAULT$}
ENDEXEC.

Effect

This static Native SQL statement sets the current connection for all following static Native SQL statements. For conn, a literal or a character-like host variable can be specified that contains the name of connection activated in the current session.

  • DEFAULT in uppercase letters or DEFAULT specified directly can be used to specify the standard connection.
  • A secondary or service connection activated using CONNECT TO without specifying a name after AS can be specified under its name conn.
  • A secondary or service connection activated using CONNECT TO while specifying a name after AS can be specified under this name name.

All of these instances are case-sensitive. When an unknown connection is specified, the current connection remains unchanged and sy-subrc is set to 4.

Notes

  • When a current connection is switched to a different current connection, no database LUW is closed and no new LUW is opened.
  • Any changes to the current connection only affect static Native SQL after EXEC SQL. and any other variant of Native SQL remain unaffected.
  • The current connection is switched regardless of whether the connections involved are active or inactive after their database LUWs are closed.

Determining the Connection

EXEC SQL.
  GET CONNECTION :conn
ENDEXEC.

Effect

This static Native SQL statement assigns the name of the current connection to conn. conn expects a character-like host variable. If the current connection was activated using the statement CONNECT TO and AS was used to give it a name at the same time, this name is assigned. If the connection is activated without being given a name, the name of the secondary connection or service connection is assigned. If the current connection is the standard connection, the value DEFAULT is assigned.

Note

The current connection can be active or inactive after its database LUW is closed.

Closing a Connection

EXEC SQL.
  DISCONNECT conn
ENDEXEC.

Effect

This static Native SQL statement closes the connection conn for the current work process, which discards all database changes not yet committed using a database commit. For conn, a literal or a character-like host variable can be specified that contains the name of a secondary connection or service connection activated in the internal session.

  • A secondary or service connection activated using CONNECT TO without specifying a name after AS can be specified under its name conn.
  • A secondary or service connection activated using CONNECT TO while specifying a name after AS can be specified under this name name.

All other specifications, most specifically the value DEFAULT, produce a runtime error. If the closed secondary connection or service connection is the current connection, the standard connection is set as the new current connection implicitly. All of these instances are case-sensitive.

Notes

  • The closed connection can be active or inactive after its database LUW is closed.
  • It is recommended that database connections are only closed implicitly by the ABAP runtime framework and not explicitly, since it takes a lot of resources to restore a connection.

Example

Example

Opening of a connection to an SAP HANA database and importing of all entries of a column in the database table SCARR.






General Data in Customer Master   ABAP Short Reference  
This documentation is copyright by SAP AG.

Length: 9756 Date: 20240328 Time: 125024     sap01-206 ( 158 ms )