ABAP 에는 pointer 개념의 reference 와 field-symbol 있는데,
그 사용법이 참... 까다롭네요.

reference는 주로 파라미터 전달을 위해 사용합니다. 참조 주소를 전달할 수 있기 때문이죠.
generic reference로 값을 받으면 읽기 위해선 field-symbol 을 사용해야 한다는 부분을 주의하세요.

REPORT  y_ref_fs_data.




DATA: lt_sflight TYPE TABLE OF sflight,
      ls_sflight TYPE sflight,
      lv_carrid TYPE sflight-carrid,
      ltr_sflight TYPE REF TO sflight_tab1,
      lsr_sflight TYPE REF TO sflight,
      lvr_carrid TYPE REF TO sflight-carrid,
      lr_data TYPE REF TO data.   " generic type ref
FIELD-SYMBOLS: <lt_sflight> TYPE sflight_tab1,
               <ls_sflight> TYPE sflight,
               <lv_carrid> TYPE sflight-carrid,
               <lt_data> TYPE ANY TABLE, " generic table type <fs>
               <ls_data> TYPE data,  " generic type <fs>
               <l_data> TYPE data.   " generic type <fs>


 

* select data.
SELECT * FROM sflight INTO TABLE lt_sflight.
READ TABLE lt_sflight INDEX 1 INTO ls_sflight.
lv_carrid = ls_sflight-carrid.


 

* 1. data -->  ref
GET REFERENCE OF lt_sflight INTO ltr_sflight.
GET REFERENCE OF ls_sflight INTO lsr_sflight.
GET REFERENCE OF lv_carrid INTO lvr_carrid.
*  or
GET REFERENCE OF lt_sflight INTO lr_data.
GET REFERENCE OF ls_sflight INTO lr_data.
GET REFERENCE OF lv_carrid INTO lr_data.
** error !!! Syntax Error !!!
*GET REFERENCE OF ls_sflight INTO ltr_sflight.
*GET REFERENCE OF lv_carrid INTO ltr_sflight.
*GET REFERENCE OF lv_carrid INTO lsr_sflight.


 

* 2. ref  -->  <fs>
ASSIGN ltr_sflight->* TO <lt_sflight>.
ASSIGN lsr_sflight->* TO <ls_sflight>.
ASSIGN lvr_carrid->* TO <lv_carrid>.
*  or
ASSIGN ltr_sflight->* TO <l_data>.
ASSIGN lsr_sflight->* TO <l_data>.
ASSIGN lvr_carrid->* TO <l_data>.
*  or
ASSIGN lr_data->* TO <l_data>.
** error !!! Syntax Error !!!
*ASSIGN lsr_sflight->* TO <lt_sflight>.
*ASSIGN lvr_carrid->* TO <lt_sflight>.
** error !!! Short Dump !!!
*ASSIGN lr_data->* TO <lt_sflight>.


 

* 3. <fs>  -->  data
MOVE <lt_sflight> TO lt_sflight.   " lt_sflight = <lt_sflight>.
MOVE <ls_sflight> TO ls_sflight.   " ls_sflight = <ls_sflight>.
MOVE <lv_carrid> TO lv_carrid.    " lv_carrid = <lv_carrid>.
* or
MOVE <l_data> TO lv_carrid.     " lv_carrid = <l_data>.
** error !!! Syntax Error !!!
*MOVE <lt_sflight> TO ls_sflight.   " ls_sflight = <lt_sflight>.
** error !!! Short Dump !!!
*MOVE <l_data> TO lt_sflight.    " lt_sflight = <l_data>.
*MOVE <l_data> TO ls_sflight.    " ls_sflight = <l_data>.


 

* 4. data -->  <fs>
ASSIGN lt_sflight TO <lt_sflight>.
ASSIGN ls_sflight TO <ls_sflight>.
ASSIGN lv_carrid TO <lv_carrid>.
*  or
ASSIGN lt_sflight TO <l_data>.
ASSIGN ls_sflight TO <l_data>.
ASSIGN lv_carrid TO <l_data>.
** error !!! Syntax Error !!!
*ASSIGN ls_sflight TO <lt_sflight>.
*ASSIGN lv_carrid TO <lt_sflight>.


 

* 5. <fs>  -->  ref
GET REFERENCE OF <lt_sflight> INTO ltr_sflight.
GET REFERENCE OF <ls_sflight> INTO lsr_sflight.
GET REFERENCE OF <lv_carrid> INTO lvr_carrid.
*  or
GET REFERENCE OF <lt_sflight> INTO lr_data.
GET REFERENCE OF <ls_sflight> INTO lr_data.
GET REFERENCE OF <l_data> INTO lr_data.
** error !!! Syntax Error !!!
*GET REFERENCE OF <lt_sflight> INTO lsr_sflight.
*GET REFERENCE OF <lt_sflight> INTO lvr_carrid.
** error !!! Short Dump !!!
*GET REFERENCE OF <ls_sflight> INTO ltr_sflight.
*GET REFERENCE OF <lv_carrid> INTO ltr_sflight.
*GET REFERENCE OF <lv_carrid> INTO lsr_sflight.


 

* 6. ref  -->  data
MOVE ltr_sflight->* TO lt_sflight.   " lt_sflight = ltr_sflight->*.
MOVE lsr_sflight->* TO ls_sflight.   " ls_sflight = lsr_sflight->*.
MOVE lvr_carrid->* TO lv_carrid.     " lv_carrid = lvr_carrid->*.
** error !!! Syntax Error !!!
*MOVE lr_data->* TO lt_sflight.   " lt_sflight = lr_data->*.
*MOVE lr_data->* TO ls_sflight.   " ls_sflight = lr_data->*.
*MOVE lr_data->* TO lv_carrid.    " lv_carrid = lr_data->*.





* 7. How to read generic ref (table)
GET REFERENCE OF lt_sflight INTO lr_data.   " generic ref
* -->  lt_sflight
ASSIGN lr_data->* TO <l_data>.
MOVE <l_data> TO lt_sflight.   " lt_sflight = <l_data>.
*  or
ASSIGN lr_data->* TO <lt_data>.
MOVE <lt_data> TO lt_sflight.   " lt_sflight = <lt_data>.
*  or
ASSIGN lr_data->* TO <lt_sflight>.
MOVE <lt_sflight> TO lt_sflight.   " lt_sflight = <lt_sflight>.
* -->  ls_sflight
ASSIGN lr_data->* TO <lt_data>.
LOOP AT <lt_data> ASSIGNING <ls_data>.
  MOVE <ls_data> TO ls_sflight.   " ls_sflight = <l_data>.
ENDLOOP.
*  or
ASSIGN lr_data->* TO <lt_sflight>.
LOOP AT <lt_sflight> ASSIGNING <ls_sflight>.
  MOVE <ls_sflight> TO ls_sflight.    " ls_sflight = <ls_sflight>.
ENDLOOP.
* -->  lv_carrid
ASSIGN lr_data->* TO <lt_data>.
LOOP AT <lt_data> ASSIGNING <ls_data>.
  ASSIGN COMPONENT 'CARRID' OF STRUCTURE <ls_data> TO <l_data>.
  MOVE <l_data> TO lv_carrid.   " lv_carrid = <l_data>.
ENDLOOP.
*  or
ASSIGN lr_data->* TO <lt_sflight>.
LOOP AT <lt_sflight> ASSIGNING <ls_sflight>.
  ASSIGN COMPONENT 'CARRID' OF STRUCTURE <ls_sflight> TO <lv_carrid>.
  MOVE <lv_carrid> TO lv_carrid.    " lv_carrid = <lv_carrid>.
ENDLOOP.


 

* 8. How to read generic ref (structure)
GET REFERENCE OF ls_sflight INTO lr_data.   " generic ref
* -->  ls_sflight
ASSIGN lr_data->* TO <ls_data>.
MOVE <ls_data> TO ls_sflight.   " ls_sflight = <ls_data>.
*  or
ASSIGN lr_data->* TO <ls_sflight>.
MOVE <ls_sflight> TO ls_sflight.    " ls_sflight = <ls_sflight>
* -->  lv_carrid
ASSIGN lr_data->* TO <ls_data>.
ASSIGN COMPONENT 'CARRID' OF STRUCTURE <ls_data> TO <l_data>.
MOVE <l_data> TO lv_carrid.   " lv_carrid = <l_data>.
*  or
ASSIGN lr_data->* TO <ls_data>.
ASSIGN COMPONENT 'CARRID' OF STRUCTURE <ls_data> TO <lv_carrid>.
MOVE <lv_carrid> TO lv_carrid.    " lv_carrid = <lv_carrid>.


 

* 9. How to read generic ref (variable)
GET REFERENCE OF lv_carrid INTO lr_data.   " generic ref
* -->  lv_carrid
ASSIGN lr_data->* TO <l_data>.
MOVE <l_data> TO lv_carrid.   " lv_carrid = <l_data>.
*  or
ASSIGN lr_data->* TO <lv_carrid>.
MOVE <lv_carrid> TO lv_carrid.    " lv_carrid = <lv_carrid>.


 

** End.

+ Recent posts