SAP/ABAP: Connection to external database
SAP, ORACLE, ABAP
1. Transaction code: ST04 - Add connection
2. Press “Add DB Entry” and provide database information
3. Transaction code: DBCO and provide database password
Example INSERT INTO database
DATA:
CONNECTION LIKE DBCON-CON_NAME VALUE 'TEST'.
Code section: Record processing:
EXEC SQL.
CONNECT TO :CONNECTION
ENDEXEC.
IF SY-SUBRC = 0.
EXEC SQL.
SET CONNECTION :CONNECTION
ENDEXEC.
EXEC SQL.
INSERT INTO Table1 (Name)
VALUES ('Adam')
ENDEXEC.
EXEC SQL.
COMMIT
ENDEXEC.
EXEC SQL.
DISCONNECT :CONNECTION
ENDEXEC.
ENDIF.
Example SELECT FROM database
TYPES:
BEGIN OF TY_TABLE1,
NAME1 TYPE C LENGTH 40
END OF TY_TABLE1.
DATA:
C1 LIKE DBCON-CON_NAME VALUE 'TEST',
S_TABLE1 TYPE TY_TABLE1,
Z_NAME TYPE C LENGTH 40.
Code section: Record processing:
Code section: Record processing:
EXEC SQL.
CONNECT TO :C1
ENDEXEC.
IF SY-SUBRC = 0.
EXEC SQL.
SET CONNECTION :C1
ENDEXEC.
EXEC SQL.
open dbcursor for select distinct Name
from Table1
ENDEXEC.
DO.
CLEAR S_TABLE1 .
EXEC SQL.
FETCH NEXT dbcursor INTO :S_TABLE1
ENDEXEC.
if sy-subrc = 0.
Z_NAME = S_TABLE1-NAME1.
else.
exit.
endif.
ENDDO.
EXEC SQL.
close dbcursor
ENDEXEC.
EXEC SQL.
DISCONNECT :C1
ENDEXEC.
ENDIF.
No comments:
Post a Comment