How to create Secondary Database connections in SAP ABAP

In addition to the primary database, other databases can be accessed using secondary database connections.

  • Using Open-SQL:

The keyword ‚CONNECTION’ and the database connection name is specified in the query. The connections are maintained in the Transaction ‘DBA COCKPIT’.

Syntax: SELECT ….. FROM db_table CONNECTION connection_name INTO ….

Example: The Secondary Database name is secondary_db

DATA ls_contact TYPE ztt_db_table2.

SELECT * FROM ztt_db_table2 CONNECTION secondary_db INTO ls_contact.

  • Using Native SQL or ADBC:

DATA lr_sql_statement TYPE REF TO cl_sql_statement.

DATA lr_sql_result_set TYPE REF TO cl_sql_result_set.

TRY.

    Lr_sql_statement = cl_sql_connection=>get_connection(‘secondary_db’)->create_statement( ).

    Lr_sql_result_set = lr_sql_statement->execute_query( l SELECT SINGLE * FROM ztt_db_table2 WHERE contact_id = 101 l ).

    CATCH cx_sql_exception.

ENDTRY.