这几天研究没有白费,终于搞定了,哈哈,很开心!这个只是雏形,还有很多地方可以完善!!!
*&---------------------------------------------------------------------*
*& Report ZSONG_TEST_CREATE_TR
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zsong_test_create_tr.
DATA gt_code TYPE STANDARD TABLE OF string.
DATA lv_filetable TYPE filetable. " 打开文件的信息放入此列表中
DATA lv_filename TYPE string. " 定义一字段用于放置上传的文件名称
PARAMETERS: p_file TYPE string,
p_src TYPE sobj_name DEFAULT 'SZC_PAPM',
p_tgt TYPE sobj_name DEFAULT 'SZC_PAPM2'.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. " 按F4鍵時也可觸發該事件
PERFORM f_open_filename CHANGING p_file.
START-OF-SELECTION.
PERFORM read_sql.
PERFORM create_tr.
*&---------------------------------------------
*&F_OPEN_FILENAME FOR OPEN FILE
*&---------------------------------------------
FORM f_open_filename CHANGING p_file.
DATA lv_rc TYPE i.
" 调用METHOD 打开对话框
cl_gui_frontend_services=>file_open_dialog( " OPEN FILE DIALOG 打开上传文件的对话框
EXPORTING
window_title = '文件'
file_filter = 'All Files (*.*)|*.*|NotePad Files(*.txt)|*.txt|Excel Files(*.xls)|*.xls|Word files(*.doc)|*.doc' " 过滤上传文件的类型
default_extension = '*.txt'
default_filename = '1.txt' " 默认打开的文件 "d:\Users\wei.sunqing\Desktop\1.txt
CHANGING
file_table = lv_filetable " 你打开文件名的列表
rc = lv_rc ). " 返回打开文件的数量
IF sy-subrc <> 0.
MESSAGE 'FILE DOES NOT EXIST!' TYPE 'E'.
ELSEIF lv_filetable[] IS NOT INITIAL. " 不為空
READ TABLE lv_filetable INDEX 1 INTO lv_filename. " 读取打開文件内表的第一个文件信息
p_file = lv_filename.
ENDIF.
ENDFORM.
*&---------------------------------------------
*&read_sql
*&---------------------------------------------
FORM read_sql.
DATA data_tab TYPE STANDARD TABLE OF itab.
cl_gui_frontend_services=>gui_upload( EXPORTING filename = p_file
filetype = 'ASC'
codepage = '8400'
CHANGING data_tab = gt_code
EXCEPTIONS file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
no_authority = 6
unknown_error = 7
bad_data_format = 8
unknown_dp_error = 12
access_denied = 13
OTHERS = 17 ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form CREATE_TR
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM create_tr.
DATA i_source TYPE rstran_s_tlogo.
DATA i_targert TYPE rstran_s_tlogo.
DATA ls_rule TYPE rstran_s_rule.
DATA:lv_subrc TYPE sy-subrc.
i_source-tlogo = 'ADSO'.
i_source-objnm = p_src.
i_targert-tlogo = 'ADSO'.
i_targert-objnm = p_tgt.
DATA(lr_tran) = cl_rstran_trfn=>factory( i_s_source = i_source
i_s_target = i_targert
i_for_delete = rs_c_true
i_expert = rs_c_true
i_new = rs_c_true ).
* lr_tran->set_force_generate_amdp( i_frc_gen_amdp = abap_true ).
TRY.
CALL METHOD lr_tran->set_mode
EXPORTING
i_mode = rsawc_c_tfc-create.
* lr_tran->set_current_version( i_objvers = rs_c_objvers-new ).
CATCH cx_rstran_display_only .
ASSERT 1 = 2. " could not happen
ENDTRY.
lr_tran->set_vers_with_proposal( rs_c_true ).
lr_tran->set_current_version( i_objvers = rs_c_objvers-new ).
lr_tran->if_rso_tlogo_maintain~save( EXPORTING i_with_cto = abap_false IMPORTING e_subrc = lv_subrc ).
WRITE / lv_subrc.
lr_tran->if_rso_tlogo_maintain~activate( EXPORTING i_with_cto = rs_c_false
IMPORTING e_subrc = lv_subrc ).
WRITE / lv_subrc.
*
lr_tran->set_haap_hint( 'Y' ) .
**get rules
* lr_tran->get_info(
* IMPORTING
* e_t_tranrule = DATA(l_tab_old_rule)
* ).
*
** Delete all rules first
* LOOP AT l_tab_old_rule ASSIGNING FIELD-SYMBOL(<l_str_old_rule>).
*
* lr_tran->delete_rule( i_ruleid = <l_str_old_rule>-r_rule->n_ruleid ).
*
* ENDLOOP.
DATA(i_script) = | METHOD procedure BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY.| && cl_abap_char_utilities=>cr_lf
&& |outtab = select | && cl_abap_char_utilities=>cr_lf
&& | '2023009' as PERIOD,| && cl_abap_char_utilities=>cr_lf
&& | 'EN' as COUNTRY,| && cl_abap_char_utilities=>cr_lf
&& | 'A0' as BU,| && cl_abap_char_utilities=>cr_lf
&& | '10000' as SALE_ORDER,| && cl_abap_char_utilities=>cr_lf
&& | 'EA01' as REPORT_ITEM,| && cl_abap_char_utilities=>cr_lf
&& | '' as RECORDMODE,| && cl_abap_char_utilities=>cr_lf
&& | 21 as BALANCE,| && cl_abap_char_utilities=>cr_lf
&& | 0 as record,| && cl_abap_char_utilities=>cr_lf
&& | 0 as SQL__PROCEDURE__SOURCE__RECORD| && cl_abap_char_utilities=>cr_lf
&& | from :intab;| && cl_abap_char_utilities=>cr_lf
&& |errortab =| && cl_abap_char_utilities=>cr_lf
&& | SELECT '' AS ERROR_TEXT,| && cl_abap_char_utilities=>cr_lf
&& | '' AS SQL__PROCEDURE__SOURCE__RECORD| && cl_abap_char_utilities=>cr_lf
&& | FROM DUMMY| && cl_abap_char_utilities=>cr_lf
&& | WHERE DUMMY <> 'X';| && cl_abap_char_utilities=>cr_lf
&& |ENDMETHOD.|.
lr_tran->set_script( i_script = i_script
i_amdp = abap_true ).
TRY.
lr_tran->create_haap( ).
CATCH cx_rstran_haap INTO DATA(l_r_rstran_haap).
ENDTRY.
lr_tran->set_vers_with_proposal( rs_c_true ).
lr_tran->set_current_version( i_objvers = rs_c_objvers-modified ).
lr_tran->if_rso_tlogo_maintain~save( EXPORTING i_with_cto = abap_false IMPORTING e_subrc = lv_subrc ).
WRITE / lv_subrc.
lr_tran->if_rso_tlogo_maintain~activate( EXPORTING i_with_cto = rs_c_false
IMPORTING e_subrc = lv_subrc ).
WRITE / lv_subrc.
lr_tran->generate( ).
ENDFORM.