메뉴 건너뛰기

SAP 한국 커뮤니티

[ABAP] Dynamic SQL ( Exception Handling )

문바이 2011.11.18 15:42 조회 수 : 1039

이글은 자료실에 있는 DynamicOpenSql 영문파일을 정리한 것이다.

 

Dynamic Open SQL.

내역 : Exception Handling (예외처리).
---------------------------------------------------------------------
사전지식 :  Oracle Native SQL사용가능( 집합함수를 사용할 줄 안다.)
            Oracle Procedure Exception처리를 해본 경험이 있다.
            또는   Java/C++  Exception처리를 해본 경험이 있다.
---------------------------------------------------------------------

기술 : 가) Exception Handling
       DATA : xref        type ref to cx_dynamic_check. (내가 처리할 exception 유형을 선언한다)
        try.  --> 예외처리 block의 시작을 말한다.
            "명령을 수행한다.(잘못된 명령으로 exception이 발생한다면 아래 catch블럭으로 이동한다)
            create data dref type table of (tabname).
            assign dref->* to <itab>.

           "오류가 발생하지 않았다면 데이타를 읽어들인다.
            select *
              from (tabname) up to 20 rows
              into table <itab>.

           "오류가 발생하지 않았다면 catch를 만나는 순간 endtry 밖으로 이동한다.
           "오류가 발생하였다면 catch로 바로 온다.
          catch cx_sy_dynamic_osql_error
                cx_sy_create_data_error into xref.
            message_txt = xref->get_text( ).
      **      i = xref->get_id( ).
            message message_txt type 'E'.
        endtry.  --> 예외처리 block의 종료를 말한다.


생각 : Dynamic SQL을 사용한다는 것은 Runtime에 예측가능하지 못한 상황을 접할 가능성이 있다는 것을 시사한다
       따라서 이와같이 경우에 ABAP의 exception handing기술을 사용해서 오류를 처리해야만
       사용자가 Dump 뜨는 화면을 접하는 것을 막을 수 있다.
       이 방법을 조금만 응용하면 여러가지 Dump 화면을 제어할 수 있으므로 특히 유심히 보자

프로그램 원문.

--------------------------------------------------------------------*

REPORT   YTEST_DYNAMIC_OPEN_SQL03.


parameter tabname type tabname.

data: dref        type ref to data,
      xref        type ref to cx_dynamic_check,
      message_txt type string,
      i           type i.

field-symbols: <itab>      type standard table,
               <row>       type any,
               <component> type any.

start-of-selection.

  try.
* dynamically create appropriate internal table
      create data dref type table of (tabname).
      assign dref->* to <itab>.

* fetch the data
      select *
        from (tabname) up to 20 rows
        into table <itab>.

    catch cx_sy_dynamic_osql_error
          cx_sy_create_data_error into xref.
      message_txt = xref->get_text( ).
**      i = xref->get_id( ).
      message message_txt type 'E'.
  endtry.

* display the result
  loop at <itab> assigning <row>.
    new-line.
    do.
      assign component sy-index of structure <row>
        to <component>.
      if sy-subrc <> 0.
        exit. " no more components
      endif.
      write: <component>.
    enddo.
  endloop.