본문 바로가기

SAP/ABAP

[ABAP] VIEW 에 파라미터 넣는 Native SQL 예제

*&---------------------------------------------------------------------*
*& Report  ZHA400_01_OSQL_HANA_10
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZHA400_01_OSQL_HANA_10.

CONSTANTS c_con TYPE dbcon_name  VALUE 'SFLIGHT'.

TYPES :  BEGIN OF   ty_s_customers,
                     id TYPE scustom-id,
                     name TYPE scustom-name,
                     postcode TYPE scustom-postcode,
                     city TYPE scustom-city,
                     country TYPE scustom-country,
                     days_ahead TYPE i,
                 END OF  ty_s_customers,
                 ty_t_customers TYPE TABLE OF ty_s_customers.

DATA: gs_customers TYPE ty_s_customers,
            gt_customers LIKE TABLE OF gs_customers.

PERFORM get_data_template
                    CHANGING gt_customers .

LOOP AT gt_customers INTO gs_customers.
  WRITE:/ gs_customers-id, gs_customers-name, gs_customers-postcode,
                  gs_customers-city, gs_customers-country, gs_customers-days_ahead.
  CLEAR gs_customers.
ENDLOOP.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA_TEMPLATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_CT_CUSTOMERS  text
*----------------------------------------------------------------------*
FORM get_data_template
            CHANGING ct_customers TYPE ty_t_customers.
* Declarations
*********************

* ADBC Objects and Variables
  DATA: lo_con TYPE REF TO cl_sql_connection,
              lo_sql   TYPE REF TO cl_sql_statement,
              lo_result TYPE REF TO cl_sql_result_set,
              lv_sql    TYPE string,
              lr_data  TYPE REF TO data.

* Exception Handling
  DATA: lx_sql_exc TYPE REF TO cx_sql_exception,
              lv_text TYPE string.

* Processing
************************************************************
  CLEAR ct_customers.


  TRY.
* Get secondary DB Connection.
      lo_con = cl_sql_connection=>get_connection( c_con ).

* Create statement
      CREATE OBJECT lo_sql
        EXPORTING
          con_ref = lo_con.

      DATA: lv_date(12).
      lv_date = sy-datum.

      CONCATENATE '''' lv_date '''' INTO lv_date.
* create SQL statement.
* SAMPLE SQL FOR INPUT PARAMETER INTO A VIEW 
      lv_sql = | SELECT ID, NAME, POSTCODE, CITY, COUNTRY, AVG_DAYS_AHEAD | &&
               | FROM _SYS_BIC."yha400.demo/CA_CUST_WITH_AVG_DAYS_AHEAD" | &&
               | ( 'PLACEHOLDER' = ( '$$FLIGHTS_BEFORE$$' , '2012-12-01') ) | &&
               | ORDER BY ID | .

* Execute Query
      lo_result = lo_sql->execute_query( lv_sql ).

* Read result into internal Table
*      GET REFERENCE OF lt_cust_book INTO lr_data.
      GET REFERENCE OF ct_customers INTO lr_data.
      lo_result->set_param_table( lr_data ).
      lo_result->next_package( ).
      lo_result->close( ).

    CATCH cx_sql_exception INTO lx_sql_exc. " Exception Class for SQL Error.
      lv_text = lx_sql_exc->get_text( ).
      MESSAGE lv_text TYPE 'E'.
  ENDTRY.

ENDFORM.                    " GET_DATA_TEMPLATE