Connect to external database

Once I needed to create a program to download data fro external database into my own. It happened to be very easily.

In DBACOCKPIT transaction create a new connection to a database.

And user it in ABAP like this:

EXEC SQL.
CONNECT TO ‘BSK’
ENDEXEC.
EXEC SQL.
SET CONNECTION ‘BSK’
ENDEXEC.
EXEC SQL.
SELECT db_name() INTO :DBN FROM SVERS
ENDEXEC.
WRITE: / ‘current database name’, DBN.
EXEC SQL.
SET CONNECTION DEFAULT
ENDEXEC.
EXEC SQL.
SELECT db_name() INTO :DBN FROM SVERS
ENDEXEC.
WRITE: / ‘current database name’, DBN.

It’s basic Native SQL в ABAP. Some SAP notes for reference.

Note 178949 – MSSQL: Database MultiConnect
Note 323151 – Several DB connections with Native SQL