ABAP 跨Client 拷贝表数据

 

REPORT  ztest_client_copy.
TYPE-POOLS:abap.
TABLES:dd02l.
SELECT-OPTIONS:s_name FOR dd02l-tabname NO INTERVALS.
PARAMETERS p_mandt1 TYPE mandt DEFAULT '600'.
PARAMETERS p_mandt2 TYPE mandt DEFAULT '700'.




TYPES:BEGIN OF ty_tname,tabname TYPE dd02l-tabname,END OF ty_tname.
DATA: lt_tname TYPE TABLE OF ty_tname ,
      ls_tname TYPE ty_tname.


DATA: d_ref TYPE REF TO data,


lt_alv_cat TYPE TABLE OF lvc_s_fcat,


ls_alv_cat LIKE LINE OF lt_alv_cat.






DATA: lt_table LIKE TABLE OF dntab.


DATA: ls_table TYPE dntab.






DATA: dyn_table TYPE REF TO data.


DATA: dyn_wa TYPE REF TO data.




FIELD-SYMBOLS :


  TYPE table,


  TYPE ANY,


  TYPE ANY,


  TYPE ANY.


*取出表结构的字段目录




*BREAK-POINT.


SELECT tabname INTO TABLE lt_tname FROM dd02l WHERE tabname IN s_name.


LOOP AT lt_tname INTO ls_tname.




  CALL FUNCTION 'NAMETAB_GET'
    EXPORTING
      langu          = sy-langu
      tabname        = ls_tname-tabname
    TABLES
      nametab        = lt_table
    EXCEPTIONS
      no_texts_found = 1.


*根据取出的字段目录生成参考字段目录


  LOOP AT lt_table INTO ls_table.


    ls_alv_cat-fieldname = ls_table-fieldname.


    ls_alv_cat-ref_table = ls_tname-tabname.


    ls_alv_cat-ref_field = ls_table-fieldname.


    APPEND ls_alv_cat TO lt_alv_cat.


    CLEAR: ls_alv_cat,
           ls_table.


  ENDLOOP.


*内表创建


  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = lt_alv_cat
    IMPORTING
      ep_table        = d_ref.


*指定生成的内表到字段符号


  ASSIGN d_ref->* TO .


*创建动态工作区结构


  CREATE DATA dyn_wa LIKE LINE OF .


*创建动态工作区


  ASSIGN dyn_wa->* TO .




*从动态表中取数到动态内表中


  SELECT * INTO CORRESPONDING FIELDS OF TABLE FROM (ls_tname-tabname)  CLIENT SPECIFIED  


WHERE mandt = p_mandt1.




  LOOP AT ASSIGNING .
    ASSIGN COMPONENT 'MANDT' OF STRUCTURE TO .
    = p_mandt2.
*    append to .
  ENDLOOP.


  MODIFY (ls_tname-tabname)  CLIENT SPECIFIED FROM TABLE .




  IF sy-subrc = 0.
    WRITE:/  ls_tname-tabname , ' copy successfully'.


  ENDIF.


  CLEAR: ls_tname,lt_table,ls_table,lt_alv_cat,ls_alv_cat.
  free d_ref.
ENDLOOP.

你可能感兴趣的:(sap)