Beruflich Dokumente
Kultur Dokumente
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
SAP YARD
TUTORIALS
CODE SNIPPETS
HELP FORUM
SAP QUIZ
79
Shares
Enteremail
Subscribe
SEARCH
http://www.sapyard.com/abaponsaphanapartviii/
1/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
SEPTEMBER 7, 2016
SAPYard
EXEC SQL.
<Native SQL statements to perform the requirement>
ENDEXEC.
656likes
Liked
Youand2otherfriendslikethis
Abapinho
7MostPopular&FeaturedArticles
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
areas and variables (line in open SQL) which have additional : (colon) in
front.
For the sake of clarity:
Fiori App - An
Introduction from an
ABAPer
EXEC SQL.
SELECT matnr mtart bismt
INTO :wa_mara
79
Shares FROM mara
WHEREmatnr = :p_matnr
ENDEXEC.
The above example does not justify the usage of native SQL, as MARA
should reside at both places. Just replace MARA with something like
ORA_INV_MGT table which is not available in SE11.
So, in the above
example concentrate on : P_MATNR and : WA_MARA (the host variables).
Let us also recap the salient features of Open SQL
1. Open SQL provides a uniform syntax and semantics for all of the database
systems supported by SAP. Therefore it is called Open. Open to all Database.
What does the above statement mean?
ABAP Programs that only use Open SQL statements will work in any SAP
system, regardless of the below database system.
2. Open SQL statements can only work for database tables that have been
created/replicated in the ABAP Dictionary
3. Open SQL can be used via secondary database connections too
NetworkedBlogs
Blog:
SAPYard
Topics:
Abap,Sap,Hana
Followourblog
http://www.sapyard.com/abaponsaphanapartviii/
3/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
NOVEMBER 5, 2014
NOVEMBER 3, 2014
3. If you are using ADBC, do not forget to handle the client/mandt explicitly
in your code.
4. ADBC does not necessarily release the allocated memory/resource on the
DB. As a good practice, we should always close the query.
http://www.sapyard.com/abaponsaphanapartviii/
4/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
5/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
If you are in HANA, it is a good practice to test the native SQL in SQL editor
at HANA Studio.
Also Read: Know about SAP HANA Studio icons and buttons
If the database is not HANA and you do not have SQL editor (HANA studio)
do not be disheartened. You can still check the native SQL at DBACOCKPIT.
It is shown a little below in this article.
For HANA Database user, your rst ADBC program is below. The ADBC API
in the program is self-explanatory and easy to implement. So, EXEC SQL
ENDEXEC would de nitely be the thing of the past. This program is for
those lucky ones who are already in HANA database. Others can scroll down
below to nd the program for the non-HANA system. This program would
not return any result if you are not in HANA, as the native SQL is dependent
http://www.sapyard.com/abaponsaphanapartviii/
6/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
on the database. The native SQL written below is compatible with HANA
only.
79
Shares * Data declaration
DATA: lr_sql_connection TYPE REF TO cl_sql_connection,
lr_sql_statement TYPE REF TO cl_sql_statement,
lr_sql_result_set TYPE REF TO cl_sql_result_set,
lr_sql_exception TYPE REF TO cx_sql_exception,
lr_sql_parameter_invalid TYPE REF TO cx_parameter_invalid,
lr_parameter_invalid_type TYPE REF TO cx_parameter_invalid_type,
lr_salv_exception TYPE REF TO cx_salv_msg,
lr_salv_alv TYPE REF TO cl_salv_table,
lt_result TYPE STANDARD TABLE OF ty_result,
ls_result TYPE ty_result,
lr_data TYPE REF TO data,
lv_where_clause_statement TYPE string,
lv_error_text TYPE string,
lv_where_mandt TYPE string,
lv_where_spras TYPE string.
* Selection screen fields
SELECT-OPTIONS : s_matnr FOR ls_result-matnr,
s_mtart FOR ls_result-mtart.
http://www.sapyard.com/abaponsaphanapartviii/
7/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
* Using the reference of the statement call, the respective methods to execute the query
* 4. Issue Native SQL Call
PERFORM issue_native_sql_call.
Shares
* 6. Retrieve Result set
PERFORM retrieve_complete_result_set.
* 8. Close DB Connection
PERFORM close_db_connection.
* 9. Display output
PERFORM display_result.
**&---------------------------------------------------------------------*
**& Sub Routines
**&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form MAKE_DB_CONNECTION
*&---------------------------------------------------------------------*
* Connect to database
*----------------------------------------------------------------------*
FORM make_db_connection .
TRY.
* Get the DB (HANA/Non HANA) Connection
* If we do not pass the DB name, it would pull the default database
lr_sql_connection ?= cl_sql_connection=>get_connection( ).
8/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
ENDTRY.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form INI_SQL_STATEMENT
*&---------------------------------------------------------------------*
* Instantiate the statement object
*----------------------------------------------------------------------*
FORM ini_sql_statement .
IF lr_sql_connection IS BOUND.
TRY.
* Get the SQL Statement reference using the instance of the connection
CREATE OBJECT lr_sql_statement
EXPORTING
con_ref = lr_sql_connection. " Database Connection
http://www.sapyard.com/abaponsaphanapartviii/
9/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
ENDTRY.
ELSE.
MESSAGE 'No connection established' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form PREPARE_NATIVE_SQL_STRING
*&---------------------------------------------------------------------*
* Construct the SQL using Concatenate syntax or string operation
*----------------------------------------------------------------------*
FORM prepare_native_sql_string .
* In line data declaration and converting selection option to a where clause string for S_MATNR
DATA(lr_seltab) = cl_lib_seltab=>new( it_sel = s_matnr[] ).
DATA(lv_where_clause_sel) = lr_seltab->sql_where_condition( iv_field = 'M.MATNR'
* In line data declaration and converting selection option to a where clause string for S_MTART
DATA(lr_seltab2) = cl_lib_seltab=>new( it_sel = s_mtart[] ).
DATA(lv_where_clause_sel2) = lr_seltab2->sql_where_condition( iv_field = 'M.MTART'
*--------------------------------------------------------------------*
* Begin of script for HANA Database
*--------------------------------------------------------------------*
* Construct the SQL in SQL Console Eclipse and put it in a string ( Native SQL Only )
* Modern sysntax for concatenation
lv_where_clause_statement = | SELECT M.MATNR, M.MTART, T.MAKTX |
http://www.sapyard.com/abaponsaphanapartviii/
10/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
79
** Modern sysntax for Concatenation
Shares
* lv_where_mandt = |'| && |{ sy-mandt }| && |'|.
* lv_where_spras = |'| && |{ sy-langu }| && |'|.
*
* lv_where_mandt = |M.MANDT = | && | { lv_where_mandt }|.
* lv_where_spras = |T.SPRAS = | && | { lv_where_spras }|.
*
**--------------------------------------------------------------------*
** Begin of script for ORACLE Database
**--------------------------------------------------------------------*
** Construct the SQL in SQL Console Eclipse and put it in a string ( Native SQL Only )
* lv_where_clause_statement = | SELECT M.MATNR, M.MTART, T.MAKTX |
* && | FROM MARA M, MAKT T |
* && | WHERE M.MATNR = T.MATNR |
* && | AND { lv_where_mandt } |
* && | AND { lv_where_spras } |
* && | AND { lv_where_clause_sel } |
* && | AND { lv_where_clause_sel2 } |.
**--------------------------------------------------------------------*
** End of script for ORACLE Database
**--------------------------------------------------------------------*
11/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
79
ENDFORM.
Shares
*&---------------------------------------------------------------------*
*& Form ISSUE_NATIVE_SQL_CALL
*&---------------------------------------------------------------------*
* Issue Native SQL Call
*----------------------------------------------------------------------*
FORM issue_native_sql_call .
TRY.
* Using the reference of the statement call the respective methods to execute the query
lr_sql_statement->execute_query(
EXPORTING
statement = lv_where_clause_statement " SELECT Statement Being Executed
hold_cursor = space
RECEIVING
result_set = lr_sql_result_set ). " Database Cursor
12/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
ENDTRY.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form ASSIGN_TARGET_RESULT
*&---------------------------------------------------------------------*
79
* Assign Target variable for result set
Shares
*----------------------------------------------------------------------*
FORM assign_target_result .
TRY.
ENDTRY.
http://www.sapyard.com/abaponsaphanapartviii/
13/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
ENDFORM.
*&---------------------------------------------------------------------*
*& Form RETRIEVE_COMPLETE_RESULT_SET
*&---------------------------------------------------------------------*
* Retrieve Result set
*----------------------------------------------------------------------*
FORM retrieve_complete_result_set .
TRY.
lr_sql_result_set->next_package( ).
79
Shares
CATCH cx_parameter_invalid_type INTO lr_parameter_invalid_type.
lv_error_text = lr_parameter_invalid_type->get_text( ).
MESSAGE e000 WITH lv_error_text.
ENDTRY.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form CLOSE_QUERY
*&---------------------------------------------------------------------*
* Close the query, release resources
*----------------------------------------------------------------------*
FORM close_query .
lr_sql_result_set->close( ).
ENDFORM.
http://www.sapyard.com/abaponsaphanapartviii/
14/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
*&---------------------------------------------------------------------*
*& Form CLOSE_DB_CONNECTION
*&---------------------------------------------------------------------*
* Close DB connection
*----------------------------------------------------------------------*
FORM close_db_connection .
lr_sql_connection->close( ).
ENDFORM.
*&---------------------------------------------------------------------*
79
*& Form DISPLAY_RESULT
Shares
*&---------------------------------------------------------------------*
* Display ALV
*----------------------------------------------------------------------*
FORM display_result .
ENDFORM.
http://www.sapyard.com/abaponsaphanapartviii/
15/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
79
Shares
For other Database users, your rst ADBC program is the same as above with
little change. Native SQL is not platform independent. In order to make the
native SQLcompatible with ORACLE database, just comment the code in
between below two tags for HANA.
**
* Begin of script for HANA Database
**
**
* End of script for HANA Database
**
And uncomment the tags in between the below two tags for ORACLE
database.
**
* Begin of script for ORACLE Database
**
**
* End of script for ORACLE Database
**
Program to demonstrate ADBC using non-HANA (Oracle) database. ADBC
Usage for Oracle DB
http://www.sapyard.com/abaponsaphanapartviii/
16/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
Let us check the output for the same program with ORACLE database users.
http://www.sapyard.com/abaponsaphanapartviii/
17/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
79
Shares
DBACOCKPIT
If you are in HANA database, you can easily check the syntax of native SQL
in SQL editor at HANA Studio. But if you do not have HANA database, you
can check the native SQL of your database using t-code DBACOCKPIT. Just
follow the path shown in below image. Execute or hit F8 and if there is any
issue in the SQL, you can easily nd them in the error/message log window
at the bottom.
http://www.sapyard.com/abaponsaphanapartviii/
18/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
79
Shares
Check the native SQL for ORACLE database. The JOIN statement for ORACLE
is different. There is no explicit JOIN command. Two tables to be joined are
separated by comma.
. I had to waste few hours just to gure this out (as I
have no ORACLE SQL experience) :). Also, check elds selected are
separated by comma and there is no Tilda (as in open SQL joins).
http://www.sapyard.com/abaponsaphanapartviii/
19/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
20/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
elds. So, if someone does SELECT * from system YARD2 (which as 2 extra
elds), then there would be problem as the Database and ABAP system
information are not same.
79
Shares
21/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
Do you have anything to add to this article? Have you faced any issue using
ADBC? Do you want to share any real project requirement or solutions?
Please do not hold back. Please leave your thoughts in the comment section.
Thank you very much for your time!!
Also, check our complete list of ABAP for SAP HANA Tutorials.
79
Shares
29
http://www.sapyard.com/abaponsaphanapartviii/
22/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
ABAP on SAP
HANA. Part VI
79
Shares
Previous post
4 COMMENTS
Next post
http://www.sapyard.com/abaponsaphanapartviii/
23/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
http://www.sapyard.com/abaponsaphanapartviii/
24/25
10/30/2016
ABAPonSAPHANA.PartVIII.ADBCABAPDataBaseConnectivitySAPYard
Enteryourcommenthere...
Shares
http://www.sapyard.com/abaponsaphanapartviii/
25/25