BDC备忘
更新模式,有下列可选值(更新模式常用的是S)
"A" 异步更新。被调用程序的更新按照没有指定 COMMIT WORK 语句和 AND WAIT 附加的方式执行。
也就是说,数据更新被放到更新队列里,由另一个专门的更新进程执行,主程序一旦提交数据就继续执行,而不管提交的更新是否执行完成。这种方式比较适合于用一个事务码大量更新指定数据,比如维护主数据等。
"S" 同步更新。被调用程序的更新按照指定了 COMMIT WORK 语句和 AND WAIT 附加的方式执行。
也就是说,数据更新被放到更新队列里,由专门的更新进程执行,但是主程序会等到数据提交完成,返回结果信息后才继续执行。这种方式比较适合于数据一致性要求比较高,多个不同事务码的连续处理。
"L" 本地更新。被调用程序的更新按照执行 SET UPDATE TASK LOCAL 语句的方式执行。
也就是说,数据更新在主程序所在的进程中完成,主程序必定等到被调用事务完成才继续执行。
report ZDBC no standard page heading line-size 255. data: begin of record, * data element: MATNR MATNR_001(018), "RMMG1 物料主数据维护:初始参数-原材料 MATNR即物料编号 * data element: XFELD KZSEL_01_002(001), "MSICHTAUSW 视图选择的帮助结构:物料主记录 表 KZSEL复选框 * data element: MAKTX MAKTX_003(040), "物料描述-物料描述(短文本) * data element: MEINS MEINS_004(003), "常规物料数据-基本计量单位 * data element: MATKL MATKL_005(009), "物料组 * data element: MTPOS_MARA MTPOS_MARA_006(004), "普通项目类别组 end of record. *** End generated data section *** DATA: itab_out LIKE TABLE OF record WITH HEADER LINE. TABLES SSCRFIELDS."用于按键 data: bdcdata like bdcdata occurs 0 with header line."批输入:新表格字段结构 包含bdc的一些屏幕号等内容 data: messtab like bdcmsgcoll occurs 0 with header line. " SAP 系统中的信息表 *** 绘屏 *** SELECTION-SCREEN begin of block blk with frame title text-001. SKIP 1. SELECTION-SCREEN BEGIN OF LINE. SELECTION-SCREEN PUSHBUTTON 1(20) but1 USER-COMMAND download. " 定义搜索按钮 SELECTION-SCREEN END OF LINE. SKIP 1. parameters:p_typ type ctu_mode obligatory default 'N', "批处理模式 p_file like rlgrap-filename. SELECTION-SCREEN end of block blk. INITIALIZATION. PERFORM frm_init_button. "初始化下载模板按钮 AT SELECTION-SCREEN . IF SSCRFIELDS-UCOMM = 'DOWNLOAD'. " 下载模板按钮响应 PERFORM temp_excel_get USING 'ZBDC_YHY' ."从服务器下载模板 CLEAR SSCRFIELDS-UCOMM. ENDIF. "为了能有选择文件对话框 at selection-screen on value-request for p_file. perform frm_select_files. START-OF-SELECTION. perform frm_get_data. END-OF-SELECTION. "START-OF-SELECTION.执行完 但输出屏幕未显示之前 perform frm_upload_data. *&---------------------------------------------------------------------* *& Form FRM_SELECT_FILES *&---------------------------------------------------------------------* * 选择文件对话框 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM FRM_SELECT_FILES . data: l_filetab type filetable, l_waftab like line of l_filetab, l_rc type i. CALL METHOD cl_gui_frontend_services=>file_open_dialog EXPORTING window_title = '打开文件' initial_directory = 'C:/' CHANGING file_table = l_filetab rc = l_rc EXCEPTIONS file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 others = 5. if sy-subrc <> 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. exit. else. "l_filetab是个内表结构,我们现在只能单选,所以只有第一条数据。还可以多选的。 read table l_filetab into l_waftab index 1. p_file = l_waftab-filename. clear: l_filetab, l_waftab. endif. ENDFORM. " FRM_SELECT_FILES *&---------------------------------------------------------------------* *& Form FRM_GET_DATA *&---------------------------------------------------------------------* * 读取excel数据到指定内表 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM FRM_GET_DATA . data lt_excel type table of alsmex_tabline with header line. data l_index like sy-tabix. CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE' EXPORTING filename = p_file i_begin_row = '3' "开始行 i_begin_col = '1' "开始列 注意实际需求 改动 i_end_row = '50000' i_end_col = '50' TABLES intern = lt_excel "lt_excel 有3个字段: row col value.即它的一行只存储一个单元格的数据 EXCEPTIONS inconsistent_parameters = 1 upload_ole = 2 others = 3. *&& 将EXCEL格式中的数据重新整理导入到内表TAB_LOAD中 loop at lt_excel. move lt_excel-col to l_index. case l_index. when'1'. move lt_excel-value to itab_out-MATNR_001. when'2'. move lt_excel-value to itab_out-KZSEL_01_002. when'3'. move lt_excel-value to itab_out-MAKTX_003. when'4'. move lt_excel-value to itab_out-MEINS_004. when'5'. move lt_excel-value to itab_out-MATKL_005. when'6'. move lt_excel-value to itab_out-MTPOS_MARA_006. endcase. at end of row."设置内表循环触发条件,AT END OF F1: 如果字段F及F的左则全部字段的数据,与下一行数据不一致时,则执行代码。 "这里的row是lt_excel里面的字段 即row=1,2,3,....取完之后才构成完整的一条itab_out数据 append itab_out. clear: itab_out. endat. endloop. ENDFORM. " FRM_GET_DATA *&---------------------------------------------------------------------* *& Form FRM_UPLOAD_DATA *&---------------------------------------------------------------------* * 将内表数据重复bdc录屏操作 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM FRM_UPLOAD_DATA . * DATA: MESS(100) TYPE C. IF ITAB_OUT[] IS INITIAL. MESSAGE '没有数据!' TYPE 'E'. ENDIF. LOOP AT itab_out. perform bdc_dynpro using 'SAPLMGMM' '0060'. perform bdc_field using 'BDC_CURSOR' 'RMMG1-MATNR'. perform bdc_field using 'BDC_OKCODE' '=AUSW'. perform bdc_field using 'RMMG1-MATNR' itab_out-MATNR_001. perform bdc_dynpro using 'SAPLMGMM' '0070'. perform bdc_field using 'BDC_CURSOR' 'MSICHTAUSW-DYTXT(01)'. perform bdc_field using 'BDC_OKCODE' '=ENTR'. perform bdc_field using 'MSICHTAUSW-KZSEL(01)' itab_out-KZSEL_01_002. perform bdc_dynpro using 'SAPLMGMM' '4004'. perform bdc_field using 'BDC_OKCODE' '/00'. perform bdc_field using 'MAKT-MAKTX' itab_out-MAKTX_003. perform bdc_field using 'BDC_CURSOR' 'MARA-MEINS'. perform bdc_field using 'MARA-MEINS' itab_out-MEINS_004. perform bdc_field using 'MARA-MATKL' itab_out-MATKL_005. perform bdc_field using 'MARA-MTPOS_MARA' itab_out-MTPOS_MARA_006. perform bdc_dynpro using 'SAPLSPO1' '0300'. perform bdc_field using 'BDC_OKCODE' '=YES'. perform bdc_transaction using 'MM02' p_typ 'S'. ENDLOOP. ENDFORM. " FRM_UPLOAD_DATA *&---------------------------------------------------------------------* *& Form BDC_TRANSACTION *&---------------------------------------------------------------------* * 执行事务代码 并返回处理结果 *----------------------------------------------------------------------* * -->P_1128 text * -->P_1129 text * -->P_1130 text *----------------------------------------------------------------------* form bdc_transaction using tcode p_typ cupdate. data: l_mstring(480). data: l_subrc like sy-subrc.. refresh messtab. call transaction tcode using bdcdata mode p_typ update cupdate "更新模式 f1可看 messages into messtab. if lines( messtab ) > 0. "如更新一条记录 可能改动多个位置 会有多个消息 我们只要最后一个消息即可 loop at messtab. if messtab-msgtyp ne 'E' and messtab-msgtyp ne 'S'. continue. endif. select single text from t100 into l_mstring where sprsl = messtab-msgspra and arbgb = messtab-msgid and msgnr = messtab-msgnr. if sy-subrc = 0. if l_mstring cs '&1'. "Contains String: True, if the content of operand2 is contained in operand1. replace '&1' with messtab-msgv1 into l_mstring. replace '&2' with messtab-msgv2 into l_mstring. replace '&3' with messtab-msgv3 into l_mstring. replace '&4' with messtab-msgv4 into l_mstring. else. replace '&' with messtab-msgv1 into l_mstring. replace '&' with messtab-msgv2 into l_mstring. replace '&' with messtab-msgv3 into l_mstring. replace '&' with messtab-msgv4 into l_mstring. endif. condense l_mstring. write: / itab_out-MATNR_001,l_mstring. CLEAR: itab_out. endif. endloop. endif. refresh bdcdata. endform. " BDC_TRANSACTION *&---------------------------------------------------------------------* *& Form BDC_DYNPRO *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->PROGRAM text * -->DYNPRO text *----------------------------------------------------------------------* form bdc_dynpro using program dynpro. clear bdcdata. bdcdata-program = program. bdcdata-dynpro = dynpro. bdcdata-dynbegin = 'X'. append bdcdata. endform. " BDC_DYNPRO *&---------------------------------------------------------------- form bdc_field using fnam fval. clear bdcdata. bdcdata-fnam = fnam. bdcdata-fval = fval. append bdcdata. endform. " BDC_FIELD *&---------------------------------------------------------------------* *& Form FRM_INIT_BUTTON *&---------------------------------------------------------------------* * 初始化按钮 为按钮添加图标和文本 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM FRM_INIT_BUTTON . CALL FUNCTION 'ICON_CREATE' EXPORTING NAME = 'ICON_DOCUMENT' TEXT = '下载模板' * INFO = ' ' * ADD_STDINF = 'X' IMPORTING RESULT = but1 * EXCEPTIONS * ICON_NOT_FOUND = 1 * OUTPUTFIELD_TOO_SHORT = 2 * OTHERS = 3 . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. ENDFORM. " FRM_INIT_BUTTON *下载EXCEL模板FORM *----------------------------------------------------------------------* * -->VALUE(templat) 上传的excel模板名 * <--VALUE(ls_destination) 返回excel文件模板对象 * *----------------------------------------------------------------------* FORM temp_excel_get USING template TYPE any. DATA: lo_objdata LIKE wwwdatatab, lo_mime LIKE w3mime, lc_filename TYPE string VALUE 'dbc',"默认名 lc_fullpath TYPE string , "C:\Users\yang\Desktop\文件名 lc_path TYPE string , "C:\Users\yang\Desktop\ 不包括文件名 ls_destination LIKE rlgrap-filename, ls_objnam TYPE string, li_rc LIKE sy-subrc, ls_errtxt TYPE string. DATA:p_objid TYPE wwwdatatab-objid, p_dest LIKE sapb-sappfad. p_objid = template. CONCATENATE lc_filename '_' SY-DATUM '_' SY-UZEIT INTO lc_filename. "给模板命名 CALL METHOD cl_gui_frontend_services=>file_save_dialog "调用保存对话框 EXPORTING default_extension = 'XLS' default_file_name = lc_filename CHANGING filename = lc_filename path = lc_path fullpath = lc_fullpath EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. IF lc_fullpath = ''. MESSAGE '不能打开excel' TYPE 'E'. ENDIF. IF sy-subrc = 0. p_dest = lc_fullpath. * concatenate p_objid '.XLS' into ls_objnam. CONDENSE ls_objnam NO-GAPS. SELECT SINGLE relid objid FROM wwwdata INTO CORRESPONDING FIELDS OF lo_objdata WHERE srtf2 = 0 AND relid = 'MI' AND objid = p_objid. *检查表wwwdata中是否存在所指定的模板文件 IF sy-subrc NE 0 OR lo_objdata-objid EQ space."如果不存在,则给出错误提示 CONCATENATE '模板文件' ls_objnam '不存在' INTO ls_errtxt. MESSAGE ls_errtxt TYPE 'I'. ENDIF. ls_destination = p_dest. "保存路径 *如果存在,调用DOWNLOAD_WEB_OBJECT 函数下载模板到路径下 CALL FUNCTION 'DOWNLOAD_WEB_OBJECT' EXPORTING key = lo_objdata destination = ls_destination IMPORTING rc = li_rc. IF li_rc NE 0. CONCATENATE '模板文件:' ls_objnam '下载失败' INTO ls_errtxt. MESSAGE ls_errtxt TYPE 'E'. ENDIF. p_file = ls_destination. "fname 全局 注意 ENDIF. ENDFORM. "fm_excel