BDC程式示例

mmccking發表於2007-09-12

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

*----------------------------------------------------------------------*

* --&gt p1 text

* p2 text

*----------------------------------------------------------------------*

FORM insert .

*這裡面的程式碼不是手動輸入的,是透過一個錄屏程式生成的,錄屏程式的TR-CODESM35,在此你還需要

*指定一個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

*----------------------------------------------------------------------*

* --&gt p1 text

* p2 text

*----------------------------------------------------------------------*

FORM process .

ENDFORM. " process

*&---------------------------------------------------------------------*

*& Form upload

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --&gt 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

*---------------------------------------------------------------------*

* --&gtPROGRAM text

* --&gtDYNPRO 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

*---------------------------------------------------------------------*

* --&gtFNAM text

* --&gtFVAL 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

*---------------------------------------------------------------------*

* --&gtFNAM text

* --&gtFVAL text

*---------------------------------------------------------------------*

FORM transaction USING tcode.

*這就是呼叫事務型別的體現,需要主意的就是模式MODEUPDATEAS的組合就是顯示所有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

*----------------------------------------------------------------------*

* --&gt p1 text

* p2 text

*----------------------------------------------------------------------*

FORM initialize_values .

*-- Get the Program Name

g_repid = sy-repid.

ENDFORM. " initialize_values

*&---------------------------------------------------------------------*

*& Form upload_data

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --&gt 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

*----------------------------------------------------------------------*

* --&gtP_0129 text

* --&gtP_0130 text

* --&gtP_0131 text

* --&gtP_0132 text

* --&gtP_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

*----------------------------------------------------------------------*

* --&gt 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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章