1,调用事物方法
2,传统方法
3,凑合着看吧~!
[@more@]REPORT Z_ABAPDEMOprojload
NO STANDARD PAGE HEADING LINE-SIZE 255.
*
*INCLUDE Z_ABAPDEMOprojload_top.
*&---------------------------------------------------------------------*
*& Include Z_ABAPDEMOPROJLOAD_TOP *
*&---------------------------------------------------------------------*
TABLES Z_ABAPDEMOproj.
DATA : BEGIN OF i_proj OCCURS 0,
project LIKE Z_ABAPDEMOproj-project,
proj_desc LIKE Z_ABAPDEMOproj-proj_desc,
stdat(10) TYPE c,
enddat(10) TYPE c,
customer LIKE Z_ABAPDEMOproj-customer,
location LIKE Z_ABAPDEMOproj-location,
END OF i_proj.
DATA i_bdctab LIKE bdcdata OCCURS 0 WITH HEADER LINE.
DATA messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
*INCLUDE Z_ABAPDEMOprojload_form.
*&---------------------------------------------------------------------*
*& Include Z_ABAPDEMOPROJLOAD_FORM *
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form insert
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* p2 text
*----------------------------------------------------------------------*
FORM insert .
*这里面的代码不是手动输入的,是通过一个录屏程序生成的,录屏程序的TR-CODE是SM35,在此你还需要
*指定一个transaction。
* perform open_group.
LOOP AT i_proj.
CLEAR i_bdctab[].
PERFORM bdc_dynpro USING 'Z_ABAPDEMO_D9_DIALOG' '0100'.
PERFORM bdc_field USING 'BDC_CURSOR'
'Z_ABAPDEMOPROJ-PROJECT'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'Z_ABAPDEMOPROJ-PROJECT'
i_proj-project.
PERFORM bdc_dynpro USING 'Z_ABAPDEMO_D9_DIALOG' '0200'.
PERFORM bdc_field USING 'BDC_CURSOR'
'Z_ABAPDEMOPROJ-LOCATION'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SAVE'.
PERFORM bdc_field USING 'Z_ABAPDEMOPROJ-PROJ_DESC'
i_proj-proj_desc.
PERFORM bdc_field USING 'Z_ABAPDEMOPROJ-STDAT'
i_proj-stdat.
PERFORM bdc_field USING 'Z_ABAPDEMOPROJ-ENDDAT'
i_proj-enddat.
PERFORM bdc_field USING 'Z_ABAPDEMOPROJ-CUSTOMER'
i_proj-customer.
PERFORM bdc_field USING 'Z_ABAPDEMOPROJ-LOCATION'
i_proj-location.
PERFORM bdc_dynpro USING 'SAPLSPO1' '0500'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=OPT1'.
PERFORM bdc_dynpro USING 'Z_ABAPDEMO_D9_DIALOG' '0200'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/EEXIT'.
PERFORM bdc_field USING 'BDC_CURSOR'
'LPROJID'.
PERFORM transaction USING 'Z_ABAPDEMOPROJ1'.
ENDLOOP.
* perform close_group.
ENDFORM. " insert
*&---------------------------------------------------------------------*
*& Form process
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* p2 text
*----------------------------------------------------------------------*
FORM process .
ENDFORM. " process
*&---------------------------------------------------------------------*
*& Form upload
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* p2 text
*----------------------------------------------------------------------*
FORM upload .
*上传文本文件,这是一个以TAB分隔的表内容文件,它的字段以我后面的数据库表字段的顺序排列
*这里使用的是GUI_UPLOAD,还可以是WS_UPLOAD,大同小异
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = 'C:Documents and Settingsxincheng24DesktopPROJ.TXT'
filetype = 'ASC'
has_field_separator = '#'
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
data_tab = i_proj
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " upload
*&--------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->PROGRAM text
* -->DYNPRO text
*---------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
*该子程序也是由录屏程序生成的
CLEAR i_bdctab.
i_bdctab-program = program.
i_bdctab-dynpro = dynpro.
i_bdctab-dynbegin = 'X'.
APPEND i_bdctab.
ENDFORM. "BDC_DYNPRO
*&--------------------------------------------------------------------*
*& Form bdc_field
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->FNAM text
* -->FVAL text
*---------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
*该子程序同样由录屏程序生成
CLEAR i_bdctab.
i_bdctab-fnam = fnam.
i_bdctab-fval = fval.
APPEND i_bdctab.
ENDFORM. "DBC_FIELD
*&--------------------------------------------------------------------*
*& Form transaction
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->FNAM text
* -->FVAL text
*---------------------------------------------------------------------*
FORM transaction USING tcode.
*这就是调用事务类型的体现,需要主意的就是模式MODE和UPDATE。AS的组合就是显示所有log
*同步更新
REFRESH messtab.
CALL TRANSACTION tcode USING i_bdctab
MODE 'A'
UPDATE 'S'
MESSAGES INTO messtab.
ENDFORM. "TRANSACTION
*Main program
START-OF-SELECTION.
*upload the data from text file to internal table
PERFORM upload.
*process the data type. mainly used to convert date type
PERFORM process.
*insert the data from internal table to database table
PERFORM insert.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
传统方法:
*&---------------------------------------------------------------------*
*& *
*& Report Zsapdemo_BDC_SESSION *
*& *
*& This program Obtain a list report for Customer Debt including each *
*& note created for the documents. *
*&---------------------------------------------------------------------*
*& Change Log: *
*& *
*&---------------------------------------------------------------------*
*& Init.| Who | Date | Text |*
*&---------------------------------------------------------------------*
REPORT zsapdemo_bdc_session
NO STANDARD PAGE HEADING
LINE-COUNT 150(5)
LINE-SIZE 135 .
*----------------------------------------------------------------------*
* Constants
*----------------------------------------------------------------------*
DATA: g_repid TYPE sy-repid,
strfilelocn TYPE string.
*----------------------------------------------------------------------*
* DB-Tables
*----------------------------------------------------------------------*
TABLES: zshilpa_1.
*----------------------------------------------------------------------*
* Variables/Internal Tables
*----------------------------------------------------------------------*
DATA: it_tab TYPE zshilpa_1 OCCURS 0 WITH HEADER LINE,
it_bdctab TYPE bdcdata OCCURS 0 WITH HEADER LINE,
it_messtab TYPE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
*----------------------------------------------------------------------*
* Select options / parameters *
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: sfileloc LIKE rlgrap-filename
DEFAULT 'C:Documents and Settingsxincheng24Desktopzshilpa_1.txt'
OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
* Initialization *
*----------------------------------------------------------------------*
INITIALIZATION.
*-- Initialize Values
PERFORM initialize_values.
*----------------------------------------------------------------------*
* Selection screen checks *
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR sfileloc.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = g_repid
IMPORTING
file_name = sfileloc.
*----------------------------------------------------------------------*
* *** MAIN PROCESSING *** *
*----------------------------------------------------------------------*
START-OF-SELECTION.
strfilelocn = sfileloc.
PERFORM upload_data.
END-OF-SELECTION.
PERFORM bdc_session.
CHECK sy-subrc IS INITIAL.
MESSAGE s002(zsapdemo_msg_class).
*&---------------------------------------------------------------------*
*& Form initialize_values
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* p2 text
*----------------------------------------------------------------------*
FORM initialize_values .
*-- Get the Program Name
g_repid = sy-repid.
ENDFORM. " initialize_values
*&---------------------------------------------------------------------*
*& Form upload_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* p2 text
*----------------------------------------------------------------------*
FORM upload_data .
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = strfilelocn
filetype = 'ASC'
has_field_separator = '#'
TABLES
data_tab = it_tab
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.
IF sy-subrc <> 0.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " upload_data
*&---------------------------------------------------------------------*
*& Form fill_bdcdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0129 text
* -->P_0130 text
* -->P_0131 text
* -->P_0132 text
* -->P_0133 text
*----------------------------------------------------------------------*
FORM fill_bdcdata USING value(bdc_program)
value(bdc_dynpro)
value(bdc_dynbegin)
value(bdc_fnam)
value(bdc_fval).
it_bdctab-program = bdc_program.
it_bdctab-dynpro = bdc_dynpro.
it_bdctab-dynbegin = bdc_dynbegin.
it_bdctab-fnam = bdc_fnam.
it_bdctab-fval = bdc_fval.
APPEND it_bdctab.
CLEAR it_bdctab.
ENDFORM. " fill_bdcdata
*&---------------------------------------------------------------------*
*& Form bdc_session
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* p2 text
*----------------------------------------------------------------------*
FORM bdc_session .
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
client = sy-mandt
group = 'Zsapdemo_BDC'
user = 'FIUSER'
prog = sy-cprog
EXCEPTIONS
client_invalid = 1
destination_invalid = 2
group_invalid = 3
group_is_locked = 4
holddate_invalid = 5
internal_error = 6
queue_error = 7
running = 8
system_lock_error = 9
user_invalid = 10
OTHERS = 11.
IF sy-subrc <> 0.
WRITE: / 'Error Occured while Opening BDC Group.'.
ENDIF.
LOOP AT it_tab.
PERFORM fill_bdcdata USING :
'SAPMSVMA' '0100' 'X' ' ' ' ',
' ' ' ' ' ' 'BDC_CURSOR' 'VIEWNAME',
' ' ' ' ' ' 'BDC_OKCODE' '=UPD',
' ' ' ' ' ' 'VIEWNAME' 'ZSHILPA_1',
' ' ' ' ' ' 'VIMDYNFLDS-LTD_DTA_NO' 'X',
'SAPLZSHILPA_1' '0001' 'X' ' ' ' ',
' ' ' ' ' ' 'BDC_CURSOR' 'ZSHILPA_1-ZMAT_NO(01)',
' ' ' ' ' ' 'BDC_OKCODE' '=NEWL',
'SAPLZSHILPA_1' '0002' 'X' ' ' ' ',
' ' ' ' ' ' 'BDC_CURSOR' 'ZSHILPA_1-ZMAN',
' ' ' ' ' ' 'BDC_OKCODE' '=SAVE',
' ' ' ' ' ' 'ZSHILPA_1-NAME' it_tab-name,
' ' ' ' ' ' 'ZSHILPA_1-ZMAT_NO' it_tab-zmat_no,
' ' ' ' ' ' 'ZSHILPA_1-ZMAN' it_tab-zman,
'SAPLZSHILPA_1' '0002' 'X' ' ' ' ',
' ' ' ' ' ' 'BDC_CURSOR' 'ZSHILPA_1-ZMAT_NO',
' ' ' ' ' ' 'BDC_OKCODE' '=SAVE',
' ' ' ' ' ' 'ZSHILPA_1-ZMAT_NO' 'it_tab-zmat_no',
' ' ' ' ' ' 'ZSHILPA_1-ZMAN' 'it_tab-zman',
'SAPLZSHILPA_1' '0002' 'X' ' ' ' ',
' ' ' ' ' ' 'BDC_CURSOR' 'ZSHILPA_1-ZMAT_NO',
' ' ' ' ' ' 'BDC_OKCODE' '=UEBE',
' ' ' ' ' ' 'ZSHILPA_1-ZMAT_NO' '2',
' ' ' ' ' ' 'ZSHILPA_1-ZMAN' '2',
'SAPLZSHILPA_1' '0001' 'X' ' ' ' ',
' ' ' ' ' ' 'BDC_CURSOR' ' ZSHILPA_1-ZMAT_NO(01)',
' ' ' ' ' ' 'BDC_OKCODE' '=BACK',
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8356764/viewspace-969062/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8356764/viewspace-969062/