SAP_BDC精华教程.docx
BDC技术介绍首先筋单解释一下BDC(BatchDataConversion):在SAP系统里.由于某种原因,可能要宣JUt入敷帚,数据不同,但是黑件是相同的,典型的情形就是切排累腕的H候,旧累优的数据要呼入SAP).大致过程不外乎建这样:输入一个I-COdC.进入一个某个屏幕,然后输入一个值(右时迁要加上一些附加的CheCkboX选项等),点执行或者确定按钮,进入另一个屏幕,在某些字段输入值(也可能是标注CheCkbOX选中或者不选中)或者修改这些字段里的值.然后按“Eneter”最后按“SAVE”.一条记录完成了,继续下一条,如此循环.上面所述的情形,如果让一个“人”来做的话,可能是真是受不了的。但是对于计算机来说,就很简隼了。SAP过一个轿殊的程序(T-Code=SHDB)把用户的一次业务施作的所有过程记录下来.从用户脩入transactioncode(事务代码).点下“StartRecording”开始.对于用户的来说是一次省通的业务操作,但是SAp却在记录:1 .光标放到寿个字段,(在履序中不用改变)2 .填入了什么值.(变量,循环中的IntenUdTab1.e某个字段)3 .点击了什么按钮(在看序中不用改变)本次操作的作为一个“代表.是一个“模板”,告诉SAP系统以怎样的方式来执行程序,也就是用计算机的话育来描述如果手工操作的话应该是怎样的一个过程。用BDC技术揭程时,有以下几个步骤:1 .把外部的It据源(Txt.Exce1.)用读Jtinterna1.SbIeJt者用doenddo循环.2 .在环里耙用SHDB记录的步*重兔执行N次,(N=DiaIog界*数量)这片实际上并没行完成数据的入,第诜只是在«*,*一个叫做-BDCDatar的InteEnTab1.e.*兆bdcdata以后.Aca1.1.transaction/XXXX,usingbdc这个令来宾正的coadt动作或<ca1.1.function'BDC_INSERT'在建立一个session.并把执行的结果返回给BeSStab这个Interna1.Tab1.e.奥在I1.i1.一肿情境,耒做一个演示的BDC也序.由于业务上的昊.某公中打算将某些料的排述郭在后面加上指定的文本GroupX所以要修改EJK存在的物料数据,用T-code:W02,外部数据是TXT文本,记录布N条,如下:Materia1.NO.6000-000012098OOOO1.2059-000092003-00003DescriptionXXXXXXX(GroupA)fdfIadsf1.adf1.(GroupB)dfdsfreddfa1fasfa(Group)iadfdafdf.adfasf(GroupD)下面是“来”操作的过程.在命令栏里输入T-COdC:SHDB,进入如下屏幕:点NCWRcsdring,出现如下屏幕:其中ReCOrding的名字是任意给的。MM2是需要浓的事务代码。按下“StartReCording*.就会进入“ChangeMateria1.",这时,以下的操作对于用户来说是一次普通修改物料插述操作,尽管系统有记录所有的操作。、&Se1.ectv1ev(5)Ocaixaton>1.Ieve1.eData输入一个物料后,点击°Se1.ectView(三)*BasicData1BwicData2Sa1.es:Sa1.esOxcData1Sa1.es:Sa1.esOxc.Data2Sa1.es:Genera1.ZP1.antDataForeignTrade:ExportDataSa1.esTextMRP*,假设我们只选"BasicRata1*和uBasicData然后点击绿色的小勾"EntcreMateria1.6000-00001autonaticXep1.enishnentnatexia1.H“辞Basicdata11.豺Basicdata21.Sa1.es:sa1.esoc*11.Sa1.es:5a1.esorc2我们在读物料后面添加一个*,GroupA",描述改为:Basicdata115B&sicdata2jSa1.es:sa1.esoxg.11Sa1.es:sa1.esorgRateria1.6000"00001autA3txcXep1.emshnentnatea1.(GxoupA)然后点-SAVE',保存这次修改.现在SAP回观了SHDB.DO三QQB先点“SAVE*,保存ReCOrding:ZteSt.然后点*Back*.返回SHDB的第一屏。选中“Ztest”,然后点击“PrograiT.SSAP臼动生成一个程序。在上面的花话框中输入程序的名字注意SRP的命名标准.丫或者Z开头).确认给人再输入一些程序的属性信忠如下:点击aSourceCode”,指定wDeveiopC1.ass"等后,就可以进入ABAPEditor,产生如下代码:rej>rtZBDCTHSTnostandardpageheading1.ine-size255.inc1.udebdcrecx1.parameters:dataset(132)1.owercase.DONOTCHANGE-thegenerateddatasection-DONOTCHANGE* Ifitisnessesarytochangethedatasectionusetheru1.es:* 1.)Eachdefinitionofafie1.dexistsoftwo1.ines* 2.)Thefirst1.ineshowsexact1.ytheCOnmen1.* ,*datae1.ement:'fo1.1.owedwiththedatae1.ement* whichdescribesthefie1.d.* Ifyoudon'thaveadatae1.ementusethe* commentwithoutadatae1.ementname* 3.)Thesecond1.ineshowsthefie1.dnameofthe* structure,thefie1.dnamemustconsistof* afie1.dnameandoptiona1.thecharacterand* threenumbersandthefie1.d1.engthinbrackets* 4.)Eachfie1.dmustbetypeC.* *Generateddatasectionwithspecificformatting-DONOTCHANGEdata:beginofrecord.* datae1.ement:MTNRMATNRJ)O1.(018),* datae1.ement:XFE1.DKZSE1.01.002(001),* datae1.ement:XFE1.DKZSE1.j)2J)O3(00D,* datae1.ement:MKTXMAKTX004(040).* datae1.ement:MEINSME1.NSJ)O5(003)、* datae1.ement:MTPOS_MARAMTPOSMARA006(004),* datae1.ement:GEWEIGEWEI_007(003).endofrecord.* *Endgenerateddatasection*start-of-se1ection.performopendatasetusingdataset,performopen_group.do.readdatasetdatasetintorecord.ifsy-subreO0.exit,endif.performbdcdynproperformbdc_fie1.dusing'Sap1.mgmm,ooeo'.using'BDC,CIRSOR,RMMG1-MATNR,.performbdc_fie1.dusing'bdjokcode',=AUSW,.performbdc_fie1.tiusing,RMMG1.-KWTNR,record-MATNR001.performbdc_dynprousing'SAP1.MGMMr'0070'.perforinbdc_fie1.dusing'bdjcursor',msiChtausw-Dytxt(02)'performbdcfie1.dusing'BDCOKCODE''=ENTR,.perforinbdc_fie1.dusing'Msichtausw-Kzse1.(OI)'record-KZSE1._01.002.performbdcfie1.dusing,MSICItTAUSW-KZSE1.(02)'record-KZSE1._02003.performbdc_dynprousing'Sap1.mgmm''4004'.performbdc_fie1.dusing'bdjokcode',=BU,.performbdc_fie1.dusing,BDCC1.iRSOR'MAKT-MAKTX'.performbdc_fie1.dUSing,MKT-MKTX'record-MAKTX004.performbdc_fie1.dusing'MARA-MEINS'record-MEINS_005.performbdc_fie1.dusing,MR-MTK)SMRA'record-MTPOSMARA006.performbdc_fie1.dusing'mrayewei'record-GEWE1._007.performbdc_transactiorusing'MM02'.enddo.performc1.ose_group.performc1.osedatasetusingdataset.上面的程序的主旻逻辑为:opendataset”读取外部覆揖源do.“循环perform填充BDCDTA子程序performbdc_transcationendo.C1.osedataset.注去上面的InCIUde程序里有两个要的form* Startnewscreenformbdc_dynprousingprogramdynpro.c1.earbdc<1.ata.bdcdata-program=program.bdcdata-dynpro=dynpro.bdcdata-dynbegin='X'.appendbdcdata.endform.Insertfie1.dformbdc-fi1.dusingfnamfva1.iffva1.Onodata.c1.earbdcdata.bdcdata-fnam=fnam.bdcdata-fva1.=fva1.appendbdcdata.endif.endform.另外还有两个重要的interna1.tab1.e声明bdcdata及oesstab* Batchinputdataofsing1.etransactiondata:bdcdata1.ikebdcdataoccurs0withheader1.ine.* messagesofca1.1transactiondata:mess1.ab1ikebdcmsgco1.1occurs0withheader1ine.选取局部最关健(典型)的代玛如下:do.readdatasetdatasetintorecord."逐行读出txt文本中的记录,请参:readdatasetifSy-SUbrCO0.exit.endif.”如出错就退出以下执行子程序(祖斜体局部)屏幕ih.perfantbdc_dynpro,后PerfoEbdc_fie1.dA、指定bdcdynpr。的实参,告知系统(HaIog程序名称:SAP1.MQiM及SCreennunber:0060PerfoEbdc_dynprousing'SAP1.MOa/''0060,.B、指定bdjfie1.d的实参,告知系统把光标放在哪个字段,这里是'RMMGI-MATNR,物料"performbdc_fie1.dusiog,BDC-CURSOR,'RtUG1.-UA,mR,.C、告知系统Okcode是什么,也可以认为是去点哪个按钮,这里是左上角的“Se1.ectYieHs)perforabdc_fie1.dusing'BDC_OKCODE',=AUSW,.这里是通过readdataset读取的外部的文件中的记录,record是一个用于临叶保存数据的仅有一行的WOrkarea。程序在运行进record-matnrOO1.应该是一个具体的物料代码。perforabdc_fie1.dusing'RtOiG1.-MATNR'record-MATNR_001.屏幕是类似的performbdc_dynproperforabdc_fie1.dusing,SAPUiQaf,0070,.using'BDC_CURSOR',ItSICHTAUSf-DnXT(02),.perfonbdc_fie1.dusing,BDC_0KC0DE',=ENTf,.perforabdc_fie1.dUSiDg'Msichtausv-Kzse1.(OI),record-KZSEk_01_002.perfonbdc_fie1.dusing,MSICHTAUSf-KZSE1.(02),record-KZSE1.02003.同样先指定程序名称及屏幕号,然后指定光标位苴,值,OK_Codeperforabdc_dynproperforabdc_fie1.dusing'SAPUKaai',4004,.using'bdcjxcode',=bu,.perfonbdc_fie1.dusing,BDC-CURSOR''MAKT-MAKTX.perfonbdc_fie1.dusing'MAKT-MAKTX,record-MAKTX_004.perfonbdc_fie1.dusing,MARA-iEINS'record-MEINS_005.perforabdc_fie1.dusing'MAM-111P0SJiARA,record-ifTP0S_MAM_006.perfonbdc_fie1.dusing,IiARA-GEWEI'record-GEHEI_007.以上的步骤实际上在一个叫做bdcda1.a的in1.erna1.IabIe中填入了一些关于谒用哪个程序(包括屏幕号码),在哪些字段中,填入什么值,点击什么按钮等信息。下面的一个步骤才真正的向系统写入数据ca1.1.function'BDCJNSERT',建立session,1股是后台运行,能给出1.Og信息或ca1.1.transaction不建立SeSSion,直接执行。执行后返回的消息保存在messtab中。perforabdc_transactionusing'JK)2,.enddo.perforac1.ose_group.perforac1.ose_datasetusingdataset.再给一个何男函*SAP动*曲程序如下(旧版SAP3U),这个程序略去了读取外部数据的程序,没有用循环,只是用Matnr从屏幕上接收用户输入的一个物料.REroRTZBDCI.tab1.esxMara.DATA:BEGINOFBDCDATAOCCURS0.INC1.UDESTRUCTUREBDCDATA.DATA:ENDOFBI)CDATA一PARAMETERS:MATNR1.IKEMARA-MATNR.PARAMETERS:PMODETYPECDEFAU1.T'N'.START-OF-SE1.ECT1.oN.PERFORMPROCESS_CCO2.PERTORMPROCESSCC03.PERFORMPROCESSCeo4.PERFORMPROCESS_CCO5.CA1.1.TRANSACTION'MM02'USINGBDCDATAMODEP.MODEUPDATE,S,.END-OF-SE1.ECTION.'Sap1.mgmm',oc6,.,BDCOKCODE'700'.'RMMG1.-MTNR'MAINR.FORMPROCESSCC02.PERFORMBDCDYNPROUSINGPERFORMBDCFIE1.DUSINGPERFORMBDCJ-IE1.DUSINGENDH)RM.'process_cc02FORMPROCESSCC03.PERFORMBIX.,DYNPROUSINGPERFORMBIX-1IIJ)IPERFORMBDC.F1E1.DENDFORM.,processcc03USINGUSING'SP1.MGMM',0070,.'BDC_OKCOI)E',ENTR,.'MsiCHTAfSW-KZSE1.(I1.)''X'Sap1.mgmm',0080,.,BDCOKCODE''ENTR,.'RMMG1.-1ERKS,TH10,.FORMPROCESSCCOI.PERFORMBDC_DYNPROUSINGPERFORMBDCFIE1.DUSINGPERFORMBDCJIE1.DUSINGENDFORM,'process/C(MFORMPROCESSCCO5.PERFORMBDC,DYNPROUSING'SAP1.MGMMr,3000,.PERFORMBDCFIE1.DUSING'BDCOKCODE,'BU'.ENDFORM,"proCeSS_CCo5FORMBDJDYNPROUSINGPROGRAMDYNPRO.C1.EARBDCDATA.Bdcdata-Program=program.Bdcdata-Dynpro=DYNPRo.Bdcdata-Dynbegin=,x'.APPENDBDCDATA.ENDFORM.FORMBDjFIE1.DUSINGENAMFVA1.CIEARBDCDATA.BDCDATA-FNAM=FNAM.B1.)CDATA-FVR1.=FVA1.APPENDBDCDATA.ENWORM.总结一下:对于一个BK程序来说,有两个困定的interna1.tab1.eWJF1.W:bdcdata$>aesstab,名环可以受,但是声典同一嵬要* Batchinputdataofsing1.etransactiondata:abc1.ikebdcdataoccurs0withheader1.ine.* messagesofca1.1.transactiondata:def1.ikebdcsgco1.1.occurs0withheader1.ine.1.ike后面的tab1.etype(这里是一个StniCtUre)是不凭改交的.有两个Hi定动施的fombdc-dynapro和bdc-fi1.d名不同样是任Jt的.便是看序代码应该是相同的,(强烈筵议不要改动,因为会给自己添加麻烦)另个还才一个Intema1.Tab1.e或者workarea是用来放从外部读遗耒的数据的.结杓应该和你的外部文件的有相关的字段偌构.一次读进全部的记录禽后循环处理1.oopatinterna1.tab1.eprocessend1.oop.或弄一条一条读进并处能后棚环读下一条do-process-end<k>.其中PrOCe8s分为两个舄彝:1.“克bdcdata表.2.Ca1.1.transactionorCa1.1.Function'BDJInser'返回消息放入Messtab.另外有一个运验之谈:1,最“柴作,的时候,不要有"多余的握作,(什么是多余的操作?自己思考吧)如果有,就重新录吧,不然这些掾作也被记录下未,程序每次都要做一些不必要的动作,2、尽量用H标去点击按钮,而不主只是故"Enter*.3.光标的位一般也可以不指定,也就是Perft>rmbdcfie1.dusinf1.,BDCCURSOR',XXXX-XXXX'通常可以不用写.不过,据说有时候去了也会出问题,如上去就没了,这个我就不知道了。呵Batchinput-RecordingWiththerecordingfunctionSAPautomatica1.1.yCdZngenereratetheBDCtab1.e.Findthetransactioncodeforthescreenthatyouwan,ttorecordOpentransactionSM35-BatchInputPushthebuttonRecordingIntheRecordingscreen,enterthennameyouwanttogivetherecording,anmdpushtheCreatebuttonYouwi1.1.nowbepromptedforthetransactioncode.Enterthetransactioncode.Pushtheenterbutton.Youarenowinthescreenwhereyouenterthetransactions.E.ts.51.ti<.sasnorma1.,and1.eavethetransactionw1.ufinished.Youwi1.1.nowhenseanoverviewoftherecording.Savetherecording.GobackPushtheOverview(Mountain)button,tosetherecording.Youcanknowgenerateaprogra三(pushtheGenerateprogrambutton)orcreateusession(PushtheCreatesessionbutton).Ifyouhavecreatedasession,gobacktotheinitia1.screenofSM35andpushtheOVCrvigbutton.Se1.ectyourbatchinputsession,andprocessiteitherinback-orforeground.Ca1.1.transactionandBatchinput-Exan1.eThisexamp1.eshowshowtouseCa1.ITransaction.IfCa1.1TransactionfaiIs.abatchinputsessioniscreated.DATA:BEGINOFBDCjrABOCCURS0.INC1.UDESTRUCTUREBDCDATA.DATA:EM)OFBDCj"AB.FORMCrcatc_Transaction.* Tab1.eformessagesfrnca1.1transaction.Thetab1.eisautomatica1.1.yfi1.1.edwithmessagsfromca1.1transaction.DATABEGINOFIneSSIabOCCURS10.INCI.UDESTRUCTUREbdcmsgco1.1.DATAENDOFmesstab.REFRESHbdc.Ub.* CreatenewdynproPERFORMbdc_ne>dynproUSING,SAPM1.03T,10.参Insertfie1.dsPERFORM1.xic-fie1.dUSINGf1.TAK-BW1.VSfw_screen1.000-io_bw1.s.PERH)RMbdcfie1.dUSING'1.TAP-MA三'wscreen1000iomatnr.PERFORMIxijfie1.dUSING'R1.03T-NIMEru_tmpstr.PERFoRMbdcJie1.dUSING'1.TRP-CHARG'wScreen1.OOOHocharg.PERFORMbdc-fie1.dUSING'BDC.OKCODB''=TM,.ndmuchmoreoftheSaBeUsethispartifyouwanttouseca1.1transaction* Ca1.1thetransaction.MessagesfromCa1.1Transactionarestoredinthe*interna1.tah1.e三esstabCA1.I.TRANSACTION,I.T0USINGbdctabMoDE'K'UPDATE'S'MESSAGESINTOmesstab.IFsysubrc=0. Ca1.1transactionsuccessfu1.1.getthenumberoftheTransferOrderthat WaScreatedI-OOPATmeSS1.db.IFBsstab-dynumb='0104'ANDaesstab-msgnr='016'.w_transportorderno=messtab-sv1.ENDIF.END1.OOP.E1.SE. Ca1.1.transaction“id.createabatchinputsessioninstead.Pi-RFORMopcnucroup.PERFORMbdc_insertUSING*1.T0.PERFORMc1.ose_group.ENDIF.ENDFORM.HerearetheStrandardformsusedforca1.1.transactionandbatchinput StartsanewscreenA*/*EORMbdc.ncwdynproUSINGprogramdynpro.C1.EARbdctab.bdc_tab-program=program.1.xic.tab-dynpro=dynpro.bdctab-dynbegi11三'X'.APPENDbdc-tab.ENDFORM.*,木*,*京*京* Insertsafie1.dinbdc_tabFORMbdc.fic1.dUSINGfna«fva1.C1.EARbdc.tab.bdc_tab-fnam=fna三,bdctab-fva1.=fva1.APPENDbdc.tab.ENDFORM.参*京*京*今*京*今*h*京*京*Opensgroup,*,*,*,*参*,*种,*,*,*12RMopen_group.CA1.1.FUNCTION'IW)J0PEN一GROuP'EXPORTINGc1.ient三sy-mandtDEST=FI1.1.ERS*group=,ZSM02,Ho1.DDATE=FI1.1.ERSkeep三'X'*user=Sy-UnameRECORD=E11.1.ER1ImtortingQIDEXCEPTIONSCIienJinVaIid=1dcstination_inva1.id=2group_inva1.id=3group_is_1.ocked=4ho1.ddatc-inva1.id=5interna1._error=6queue_error=7running=8syste«_1.ock_crror=9USeJinVa1.id三10OTHERS=11.ENDFORM.,米*京*.*.*.*/*.,*)f*.*.*C1.osesgroupar*相FORMc1.osegroup.CA1.1.FUNCTION*BDC_C1.OSE_GRO1.P,EXCEPTIONSnot_opcn=1queueerror=2OTHERS=3.ENDI-ORM.*我*«*«我*A*BDC-INSERT*>M(*)M(*FORMbdc_insertUSINGtcode.CA1.1.FUNCTI(W'BDC_JNSERT'EXPORTINGtcode*PoS1.OCA1.=tcode=Novb1.oca1.*PRINTINGTAB1.ES=NOPRINTdynprotab=bdc_tabEXCEPTIONSinterna1.error三1not_open=2queue.error=3ICOde.inva1.id=4Priritinginva1.id三5posting_inva1.id=6OTHERS=7.ENDFORM.