Beruflich Dokumente
Kultur Dokumente
view=sapnote&id=323151
http://www.dataxstream.com/2010/05/using-native-sql-in-an-abap-proxy/
***********************************************************************
TABLES dd02t.
SELECT-OPTIONS tabname FOR dd02t-tabname.
DATA:wa_tab LIKE dd02t,
i_tab LIKE TABLE OF dd02t.
EXEC SQL.
OPEN C FOR
SELECT *
FROM dd02t
WHERE ddlanguage <b>=</b> :sy-langu
ENDEXEC.
DO.
EXEC SQL.
FETCH NEXT C INTO :wa_tab
ENDEXEC.
IF sy-subrc <> 0.
EXIT.
ENDIF.
APPEND wa_tab TO i_tab.
ENDDO.
EXEC SQL.
CLOSE C
ENDEXEC.
SORT i_tab BY tabname.
DELETE i_tab WHERE NOT
tabname IN tabname.
********************************************************************************
***
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
managed with the indicated alias name. If no alias name is specified, <con_name>
is used as the identification for the open connection. If you enter different a
lias names, you can open several connections to the same database simultaneously
by using the same user ID. This alias mechanism allows the execution of indepen
dent transactions on the same logical database connection (see example 2). The a
lias name can also be entered as a literal or as a variable.
If no entry is found in the table DBCON for the indicated connection name <con_n
ame>, an ABAP runtime error occurs. If, on the other hand, the connection setup
fails due to a DB error, for example, because the relevant database is not onlin
e, then SY-SUBRC = 4 is set and the ABAP program assumes control.
After the connection has been successfully set up (SY-SUBRC = 0), the connection
that was just opened automatically turns into the active connection, that is, f
rom this point on, all subsequent Native SQL statements (and only these) will ac
cess the database associated with this connection. However, all subsequent Open
SQL accesses still refer to the default connection. If the connection is not clo
sed explicitly using a "DISCONNECT" statement (see below), it remains open until
it is closed implicitly when the corresponding internal session is closed (the
term "internal session" is explained in the section on "Data area and Modulariza
tion Unit Organization" in the ABAP online documentation). After that, you can a
ccess this connection only if it has been opened again explicitly using a new "C
ONNECT" statement.
Note:
If a database connection that was opened using a "CONNECT" statement is not expl
icitly closed again using a "DISCONNECT" statement, it is invalidated (this mean
s that it will have to be opened again using a "CONNECT" for further accesses) w
hen the internal session is closed, but the physical connection remains. The adv
antage of this is that it will not have to be set up again if a new "CONNECT" is
executed. However, "reusing" a connection that was previously opened works only
if there are no open transactions on this connection, that is, if the last tran
saction on this connection was completed using COMMIT or ROLLBACK.
Setting the active database connection
EXEC SQL.
SET CONNECTION { <alias_name> | DEFAULT }
ENDEXEC.
This statement sets the data base connection <alias_name> that has been opened t
o active. This means that all subsequent Native SQL statements are executed on t
his connection. If a transaction is still open on the previous connection, it re
mains open and can later be continued by reverting to this connection.
You can also enter the keyword DEFAULT as an alias name. This causes the R/3 def
ault connection to turn into the active connection again.
Again, <alias_name> can be entered directly as a literal or as a variable value
in form of :<var>. However, the entered alias name must be known from a precedin
g "CONNECT" statement (that is, a statement executed within the same internal mo
de). If the database connection was opened without explicitly specifying an alia
s name, the connection name <con_name> must be used in the "SET" statement as th
e <alias_name>.
If there is no opened connection for the entered connection name, SY-SUBRC = 4 i
s set.
Disconnecting a database connection
EXEC SQL.
DISCONNECT <alias_name>
ENDEXEC.
The "DISCONNECT" statement explicitly closes a database connection opened under
the <alias_name> name. As of Basis Release 6.20, the following applies: The tran
saction running on this connection is rolled back. Before Release 6.20, the phys
ical connection is retained if the transaction running on this connection was no
t completed by COMMIT/ROLLBACK. After a "DISCONNECT", you can no longer access t
his connection until it is reopened explicitly by a "CONNECT". If the closed con
nection was also the connection that was just active, the R/3 default connection
automatically turns into the active connection again.
The "DISCONNECT" statement also closes the physical connection to the database.
If you are setting up a new connection, you must first re-establish this physica
l connection, which may be a rather time-consuming operation because the relevan
t system resources will have to be requested for the client as well as for the d
atabase server. Therefore, you should explicitly close a connection using the "D
ISCONNECT" statement only if you want the actions executed on this connection to
run only occasionally. However, if you want to use this connection permanently,
it should remain open.
If the <alias_name> does not correspond to any previously opened DB connection,
an ABAP runtime error occurs.
Example 1: Accessing remote DB in a subroutine
FORM remote_access USING con_name LIKE dbcon-con_name.
* This form performs some database operations on connection 'con_name'.
* The form does not require that the caller has already opened this
* connection nor does it require that 'con_name' is "active". After
* the form has finished the "default connection" is active
* Test if connection 'con_name' has already been opened
EXEC SQL.
SET CONNECTION :con_name
ENDEXEC.
IF SY-SUBRC <> 0.
* Connection not yet opened.
EXEC SQL.
CONNECT TO :con_name
ENDEXEC.
IF SY-SUBRC <> 0.
*
error handling
ENDIF.
ENDIF.
* Do something on connection 'con_name'
EXEC SQL.
...
ENDEXEC.
* Commit the changes on 'con_name'
EXEC SQL.
COMMIT
ENDEXEC.
* Reset to "default connection"
EXEC SQL.
SET CONNECTION DEFAULT
ENDEXEC.
ENDFORM.
Example 2: Executing independent transactions
DATA con_name LIKE dbcon-con_name VALUE 'ABC'.
* Open a connection C1 to a database identified by the logical name
* 'ABC'. This implicitly starts a transaction T1.
EXEC SQL.
CONNECT TO :con_name AS 'C1'
ENDEXEC.
IF SY-SUBRC <> 0.
* error handling
ENDIF.
* Do some database operations on connection C1
EXEC SQL.
...
ENDEXEC.
* Open a second connection C2 to the same database 'ABC'. This
* implicitly starts transaction T2 which became the "active" one.
* Transaction T1 still remains open.
EXEC SQL.
CONNECT TO :con_name AS 'C2'
ENDEXEC.
IF SY-SUBRC <> 0.
* error handling
ENDIF.
* Do some operations on connection C2
EXEC SQL.
...
ENDEXEC.
* Commit transaction T2. This commit does not have any effect on
* transaction T1.
EXEC SQL.
COMMIT
ENDEXEC.
* Resume transaction T1 on connection C1
EXEC SQL.
SET CONNECTION 'C1'
ENDEXEC.
EXEC SQL.
...
ENDEXEC.
* Commit transaction T1 on connection C1
EXEC SQL.
COMMIT
ENDEXEC.
* Reset to the default connection
EXEC SQL.
SET CONNECTION DEFAULT
ENDEXEC.