SAP EXEC CONNECTION ABAP Statements



Get Example source ABAP code based on a different SAP table
  


• CONNECT TO EXEC SQL

ABAP_EXEC_SQL - 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 AB-SQL 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*.
ITOC



Latest notes:

For detailed information about database connections, see database connections.
ABAP_HINT_END

Opening a Connection

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

What does it do?
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 ABAP_ISESS , 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:
A name from the column CON_NAME of the database table DBCON used to specify a secondary connection
A name with the prefix R/3* used to specify a service connection
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 ABAP_ISESS . In an ABAP_ISESS , 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.



Latest notes:

The standard connection cannot be requested using CONNECT TO.
A name granted using AS can also be used in AB_SQL after the addition CONNECTION and in the statements COMMIT CONNECTION and ROLLBACK CONNECTION, as long as it is in uppercase letters.
ABAP_HINT_END
• SET CONNECTION EXEC SQL

Selecting a Connection

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

What does it do?
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.



Latest 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. AB_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.
ABAP_HINT_END
• GET CONNECTION EXEC SQL

Determining the Connection

ABAP_SYNTAX
EXEC SQL.
GET CONNECTION :conn
ENDEXEC.

What does it do?
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.



Latest notes:

The current connection can be active or inactive after its database LUW is closed.
ABAP_HINT_END
• DISCONNECT EXEC SQL

Closing a Connection

ABAP_SYNTAX
EXEC SQL.
DISCONNECT conn
ENDEXEC.

What does it do?
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 ABAP_ISESS .
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.



Latest 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.
ABAP_HINT_END

Example



Example ABAP Coding

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

Return to menu