Accessing views through ABAP
As discussed previously, whenever views are activated, a column view is created in the schema _SYS_BIC. The column view name contains the name of the package and the name of the view.
Example: “SYS_BIC”.”PK_AMAZON/AT_SFLIGHT_1″
In addition, a public synonym is created with the following name:
“PK_AMAZON: :AT_SFLIGHT_1”
Using these names, the views can be accessed using Native SQL in ABAP.
As of ABAP 7.4, you can also import the views from SAP HANA Repository into ABAP Data Dictionary and then access them using Open SQL.
Accessing Views through Native SQL
Here, the target structure must be defined manually in ABAP data dictionary as the view structure is not known.
The below example uses some ABAP 7.4 language elements (like constructor and inline declarations). These are not necessary but shortens the ABAP code.
Example to access the view AT_SFLIGHT_1 via ADBC
Note: refer this post to see the creation of the attribute view AT_SFLIGHT_1
" Definition of the target structure TYPES : BEGIN OF ty_data , carrid TYPE s_carr_id. connid TYPE s_conn_id . fldate TYPE s_date. route TYPE string , END OF ty_data . CONSTANTS: gc_view TYPE string VALUE 'PK_AMAZON::AT_SFLIGHT_1'. DATA : lt_data TYPE TABLE OF ty_data . " Accessing attribute view DATA( lv_statement) = | SELECT carrid, connid, fldate , route | && | FROM "{ gc_view }" | && | WHERE mandt = '{ sy-mandt }' ORDER BY fldate |. TRY . " Preparing the SOL connection and statement DATA( lo_result_set ) = cl _sql _connection=>get_connection( >->create_statement( tab_name_for_trace = conv #< gc_view ) ->execute_query( lv_statement ) . " Get result lo_result_set->set_param_table( REF#( lt_data l ) . lo_result_set ->next_package( ) . lo_result_set ->close( ) . CATCH cx_sq l_exception INTO DATA(lo_ex) . " Error handling WRITE : | { lo_ex->get_text() } |. ENDTRY . LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<l>) . WRITE : / <l>-carrid , <l>-connid, <l>-fldate , <l>-route . ENDLOOP .
The errors occurring here may be due to syntax or mismatch of ABAP data type.
External Views in ABAP Data Dictionary
In ABAP 7.4, external views are a new data type in ABAP data dictionary. These are not defined completely in ABAP data dictionary and are therefore kind of Proxy which help to access the column views in _SYS_BIC schema of the SAP HANA Repository. Therefore they have the name ‘external’.
Using external views, you can import the column views into the ABAP data dictionary.
In Eclipse, go to ABAP development tools and create an external view.
When a view is created, system checks whether it can be imported into ABAP Data Dictionary.
Please note that not all SAP HANA data types are supported in ABAP.
When accessing HANA views containing calculated attributes or accessing tables through views that are not created in ABAP data dictionary, error may occur relating to data type mismatch while creating an external view and the view cannot be imported.
View Structure and Synchronization
Once a view is imported as external view in ABAP, you can see the structure of the view together with the data type mappings. In addition, there is a button ‘SYNCHRONIZE’, which is used to update the external view whenever the view structure is changed in HANA. For example, whenever you add or delete any attributes in the corresponding view in SAP HANA studio, use the button to synchronize the external view with these changes otherwise you may get runtime error while using the external view.
SQL data types and ABAP data types cannot always be mapped uniquely, for this reason, the correct mapping should be done by the developer while creating an external view.
Please note: Column views in SAP HANA that exist only in the database catalog (e.g. generated programmatically) cannot be imported into ABAP data dictionary.
Accessing External Views
The advantage of external views is that you can use Open SQL to access the SAP HANA views.
This will have following benefits-
- ABAP code compiler can check for errors and provide automatic code completion
- Automatic client handling
- Iterating through a result set within a SELECT loop
- You can use INTO CORRESPONDING FIELDS expression
- You can use IN for the WHERE condition to transfer selection options
Below example shows how to access an external view using ABAP Open SQL. As you can see, the code is shorter as compared to native SQL discussed earlier-
REPORT ZR_VIEW_OPEN . DATA : wa TYPE zev_at_flight_1 . "Your external view " Read data from external view SELECT carrid connid fldate from_to FROM zev_at_flight_1 INTO CORRESPONDING FIELDS OF wa. WRITE : / wa-carrid , wa-connid , wa-fldate, wa-from_to . ENDSELECT.
To learn more, go for complete course in ABAP Development for SAP HANA