ABAP Keyword Documentation → ABAP - Reference → Processing External Data → ABAP - Database Accesses → Native SQL → EXEC SQL
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 AS ABAP, 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 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.
Possible AS ABAP connections to database systems are stored in the DBCON database table. You can create and change entries in the DBCON database table by using the DBA Cockpit tool.
Other versions: 7.31 | 7.40 | 7.54
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 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-like 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 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 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
AS 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 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
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.