ABAP Programming.ppt
ABAP Training,ABAP ProgrammingDavid Sun,ABAP Programming Language,Advanced Business Application Programming,ABAP,Reporting,Programs can be written interactively using the capabilities of the R/3 system.Programs can be executed both online or in the background.Background jobs can also be scheduled to run at specific intervals.,Non-SAPLegacySystem,General Programming,Module Pool Programming,Batch Input Processing,Inter-System Communication,Sequential Dataset Processing,Terms to Remember,Report:An ABAP program whose output is a list,Module Pool:A dialog program which is a collection of screens,List:The output generated by an ABAP report program,ProgramA series of ABAP statements,ABAP Editor,ABAP Editor,Reporting,ABAPDevelopment,Reporting and ABAP Development,Programming Environment-The ABAP Editor,Programming EnvironmentOnline Debugging,Basic Functions of the ABAP Editor,Program source entry area,Find andRepeat Find,Toggles fromdisplay to change mode,Undo,ABAP Help,Syntax Check,Standard toolbar,Cut,copy and paste to and from a buffer,The PROGRAM Menu Option,The EDIT Menu Option,The GOTO Menu Option,The UTILITIES Menu Option,The BLOCK/BUFFER Menu Option,The SETTINGS Menu Option,To set Editor mode,use the menu pathSettings-Editor modePreferred settings:PC mode with line numbering With compression logic Key word large,DATA COUNT TYPE I.DATA TITLE(25).MOVE 1 TO COUNT.MOVE President TO TITLE.WRITE TITLE.WRITE COUNT.,Basic ABAP Program Syntax,ABAP Program,Statement.,Word 1,Word 2,Word 3,Word 4,Key word,Parameter,field,or constant,Chaining Statements in ABAP,DATA COUNT TYPE I.DATA TITLE(25).MOVE 1 TO COUNT.MOVE President TO TITLE.WRITE TITLE.WRITE COUNT.,WRITE:TITLE,COUNT.,DATA:COUNT TYPE I,TITLE(25).,MOVE:1 TO COUNT,President TO TITLE.,Comments in ABAP,A double quotation mark(“)anywhere on a line makes everything that follows a comment.,An asterisk(*)in column1 makes the entire linea comment line.,ABAP Program Structure,Program Name AreaUse REPORT for listing programsUse PROGRAM for online programs,Declaration SectionUsed for defining tables,variablesand constants,Statement SectionUsed for coding executableABAP statement,ABAP Program Attributes,Type:1-Executable ProgramI-Include ProgramM-Module PoolF-Function GroupS-Subroutine Pool,Program title is required,Application:S-BasisU-EDM(Enterprise Data Model)V-Sales and DistributionY-Customer Head OfficeZ-Customer Branch*-For all Applications,Running an ABAP Program,From the ABAP Editor:With the program displayed,ProgramExecute,An Alternative Method:From the System Menu,choose Services Reporting to launch the program,One Final Method:From the ABAP Editor:Initial Screen,type the program name,then click on the Execute push-button,The ABAP“WRITE”Statement,WRITE*.SKIP 2.WRITE:The date today is:,SY-DATUM,The current time is:,SY-UZEIT.SKIP 2.WRITE*.,1-*The date today is:12/30/1996 The current time is:12:32:06*,Notice that there is an automatic space between the fields written to the list.,ABAP Format Specifications,WRITE 10*City of Philadelphia*.SKIP 2.WRITE:10 Ticket Date,SY-DATUM.WRITE:/10 Ticket Time,SY-UZEIT.ULINE/10(60).SKIP 2.WRITE 10*Department of Public Safety*.,*City of Philadelphia*Ticket Date 01/01/1997 Ticket Time 18:01:00-*Department of Public Safety*,ABAP Format Options,WRITE 10*City of Philadelphia*.SKIP 2.WRITE:10 Ticket Date,25 SY-DATUM,/10 Ticket Time,SY-UZEIT UNDER SY-DATUM.ULINE/10(60).SKIP 2.WRITE 10*Department of Public Safety*.,*City of Philadelphia*Ticket Date 01/01/1997 Ticket Time 18:01:00-*Department of Public Safety*,“WRITE”Statement Extended Syntax,WRITE.,/p(I)/=Line Feedp=Column PositionI=Output Length,Program Field orText Literal,Format Options,WRITE*City of Philadelphia*.SKIP.WRITE:/Ticket Date,15 SY-DATUM.WRITE:/Ticket Time,15 SY-UZEIT.SKIP.WRITE*Department of Public Safety*.NEW-PAGE.WRITE:Comments:.,1-*City of Philadelphia*Ticket Date 01/01/1997 Ticket Time 18:01:00*Department of Public Safety*2-Comments:,“NEW-PAGE”Statement,Writing Icons and Symbols,INCLUDE.INCLUDE.WRITE:/10 ICON_DATE AS ICON,SY-DATUM,SYM_LEFT_HAND AS SYMBOL.,Execute programs“SHOWICON”and“SHOWSYMB”to display the systems icons and symbols.,Maintaining Page Headers,The title comes from the programs attributes.,List header will replace title on first line of standard header.,Up to four lines of column headings can be maintained for the list.,Numbered Texts(Text Symbols),Text symbols are referenced by a unique three-character code.,WRITE:/TEXT-001,BIRTHDAY,/TEXT-002,SY-DATUM.,C:Character Text,I:Integer,P:Packed#,F:Floating Point#,N:Numeric Text,D:Date,T:Time,X:Hexadecimal#,ABAP Elementary Data Types,DATA:PLAYER(35)TYPE C,NICKNAME(35),POINTSTYPE I,GAMESTYPE IVALUE 10,AVERAGE(5)TYPE P,STARTER,ACQUIREDTYPE D.,Declaring Variables,C:(blank),I:zero,P:zero,F:zeroes,N:zeroes,D:00000000,T:000000,X:00,The“CLEAR”statement sets a field back to its initial value,not its default value.,Initial Values,DATA:PLAYER(35)TYPE C,NICKNAME(35)VALUE Dr.J,POINTSTYPE IVALUE 255,GAMESTYPE IVALUE 10,AVERAGE(5)TYPE PVALUE 25.5,STARTERVALUE Yes,ACQUIREDTYPE DVALUE 19760610.,Assigning Default Values,DATA:PLAYER(35)TYPE CVALUE Julius Erving,NICKNAME(35),ACQUIREDTYPE D.,DATA:PLAYER(35)TYPE CVALUE Julius Erving,NICKNAMELIKE PLAYER,ACQUIREDLIKE SY-DATUM.,Use the“LIKE”addition to declare fields with the same format(i.e.,data type and length),Declaring“Like”Fields,CONSTANTS:TEAM1(20)TYPE CVALUE 76ers,TEAM2LIKE TEAM1 VALUE Celtics,TOT_GAMESTYPE IVALUE 82.,If you attempt to change the value of a constant,a syntax error will occur.,The“VALUE”additionis required.,Declaring Constants,TYPES:NAME(35)TYPE C,TEAMS(20)TYPE C.DATA:PLAYERTYPE NAMEVALUE Troy Aikman,NICKNAMELIKE PLAYER.CONSTANTS:TEAM1TYPE TEAMSVALUE Cowboys,TEAM2LIKE TEAM1 VALUE Packers.,A user-defined data type created with the“TYPES”statement is used to specify a fields data type in the“TYPE”addition of the“DATA”or“CONSTANTS”statements.,User-Defined Data Types,Standard LengthC=defined lengthI=12P=(2*defined length)+1F=22N=defined lengthD=10T=8X=(2*defined length),JustificationC=left-justifiedI=right-justifiedP=right-justifiedF=right-justifiedN=left-justifiedD=left-justifiedT=left-justifiedX=left-justified,Output Characteristic for Data Types,DATA:FLOAT TYPE FVALUE 98.7654321E2,PACKTYPE PVALUE 12,INTTYPE I VALUE 32.WRITE:/FLOAT,/FLOAT EXPONENT 1 DECIMALS 3,/FLOAT EXPONENT 0 DECIMALS 2,/PACK,/PACK DECIMALS 1,/INT DECIMALS 2.,9.876543210000000E+03987.654E+019876.5412 12.0 32.00,These fields are not aligned because of the different standard output lengths of the numeric type fields.,Output for Numeric Fields,DATA:TITLE(25),SALARY TYPE P,CNVSALARY LIKE SALARY,MOVE President TO TITLE.COMPUTE SALARY=5000000.CNVSALARY=SALARY*3.ADD 1000 TO SALARY.,MOVE TO.,COMPUTE=.,ADD TO.SUBTRACT FROM.MULTIPLY BY.DIVIDE BY.,Assigning Values to Fields,COUNTER=COUNTER+1.SALARY=BASE*BONUS_PERCENT.LENGTH=STRLEN(NAME).ANSWER=(10+SQRT(NUM1)/(NUM2-10).,Spacing is very important when using arithmetic expressions!,FunctionsSQRT,EXP,LOG,SIN,COS,STRLEN,.,Operators+-*/*DIV and MOD,Arithmetic Expressions,90 xx 1996123456ABCD-06/01/1996,Use an offset and length to display or change portions of a field.,Sub-Fields in ABAP,You are 12410 days old.,Date fields store values as“YYYYMMDD”.,Date Calculations in ABAP,PARAMETERS:NUM TYPE I,NAME(20)DEFAULT AARON.ADD 10 TO NUM.WRITE:/NUM,-,NAME.,32-AARON,“Selection Screen”,Declaring Fields with PARAMETERS,These selection texts will be used on the selection screen instead of the parameter names.,Selection Texts,DATA:NUM TYPE I VALUE 12.FIELD-SYMBOLS:,TYPE I,LIKE NUM.ASSIGN:NUM TO,NUM TO,NUM TO.WRITE:/Line 1:,NUM,.=32.WRITE:/Line 2:,NUM,.,A field symbol is a“pointer”that assumes a fields address,not a fields value.,Line 1:12121212Line 2:32323232,Working with Field Symbols in ABAP,Dynamic Assignment of Partial Strings,PARAMETERS FIELD(8)DEFAULT SY-UZEIT.FIELD-SYMBOLS.ASSIGN(FIELD)TO.IF SY-SUBRC=0.WRITE:/The contents of field,FIELD,.ELSE.WRITE:/Failure assigning field,FIELD,to field symbol.ENDIF.,The contents of field SY-UZEIT 12:32:06,Selection Screen,List,Dynamic Field Assignment,Retrieving Information From the Database,Data,John Smith,Mary Stiles,Open SQL,Portable across various databases,Open SQL-a subset of standard SQL,The Basic SELECT Statement,DB,Mary Stiles,John Smith,Work Area,SELECT*FROM.ENDSELECT.,Example Using the SELECT Statement,TABLES:KNA1.SELECT*FROM KNA1.WRITE:/KNA1-KUNNR,KNA1-NAME1.ENDSELECT.,The SELECT Statement with a WHERE Clause,TABLES:KNA1.SELECT*FROM KNA1 WHERE KTOKD=0001.WRITE:/KNA1-KUNNR,KNA1-NAME1.ENDSELECT.,SY-SUBRC,TABLES:KNA1.SELECT*FROM KNA1 WHERE KTOKD=0001.WRITE:/KNA1-KUNNR,KNA1-NAME1.ENDSELECT.IF SY-SUBRC 0.WRITE:/No records found.ENDIF.,The INTO Clause,TABLES:KNA1.SELECT KUNNR,NAME1 FROM KNA1 INTO(KNA1-KUNNR,KNA1-NAME1).WRITE:/KNA1-KUNNR,KNA1-NAME1.ENDSELECT.IF SY-SUBRC 0.WRITE:/No records found.ENDIF.,INTO CORRESPONDING-FIELDS,TABLES:KNA1.SELECT KUNNR,NAME1 FROM KNA1 INTO CORRESPONDING-FIELDS OF KNA1.WRITE:/KNA1-KUNNR,KNA1-NAME1.ENDSELECT.IF SY-SUBRC 0.WRITE:/No records found.ENDIF.,Basic Flow Control in ABAP,The IF Statement,IF X=5.WRITE:/The value of X is 5.ELSEIF X=6.WRITE:/The value of X is 6.ELSE.WRITE:/X is neither 5 nor 6.ENDIF.,Logical Expressions,Logical Expressions use:RELATIONAL OPERATORSLOGICAL OPERATORSSTRING COMPARISON OPERATORS,Relational Operators,Logical Operators,The hierarchy of the logical operators is:NOT,AND and then OR.(ie different from creating views),String Comparison Operators,Comparison,Syntax,Contains onlyContains anyContains stringContains patternContains not onlyContains not anyContains no stringContains no pattern,COCACSCPCNNANSNP,The CASE Statement,The DO Loop,DO.WRITE:/Hello world!.ENDDO.,J=4.DO J TIMES.WRITE:/Hello world!.ENDDO.,The WHILE Loop,If expression evaluates to TRUE,code in loop is executed.,If expression evaluates to FALSE,code in loop is NOT executed,and control moves to after ENDWHILE.,Nested Loops and Control Structures,DO 2 TIMES.WRITE:/SY-INDEX.DO 3 TIMES.WRITE:/,SY-INDEX.ENDDO.ENDDO.,The ON CHANGE OF Statement,SELECT*FROM YCUSTOMER.ON CHANGE OF YCUSTOMER-COUNTRY OR YCUSTOMER-STATE.ENDON.ENDSELECT.,The CHECK Statement,DO 10 TIMES.CHECK SY-INDEX=4.WRITE:/SY-INDEX.ENDDO.,The EXIT Statement,IF SY-SUBRC 0.EXIT.ENDIF.,The CONTINUE Statement,DO 10 TIMES.IF SY-INDEX 4.CONTINUE.ENDIF.WRITE:/SY-INDEX.ENDDO.,Address List,Structure,Data Structures,Address List,Internal Table,Declaring a Structure-Method#1,1REPORT YN1C0008.2 3TABLES:TABNA.4DATA:BEGIN OF ADDRESS,5FLAGTYPE C,6IDLIKE TABNA-ID,7NAME1LIKE TABNA-NAME1,8CITYLIKE TABNA-CITY,9END OF ADDRESS.10MOVEXTO ADDRESS-FLAG.11MOVE0001TO ADDRESS-ID.12MOVESmithTO ADDRESS-NAME1.13MOVEPhiladelphia TO ADDRESS-CITY.1415WRITE ADDRESS.1617181920212223,Basic Syntax:DATA:BEGIN OF.END OF.,FlagID Name1 City,Address Structure,Is this statement necessary for the code?,REPORT Yxxxxxxx.TYPES:BEGIN OF ADDR,FLAG,IDLIKE EMPLOYEE-ID,NAME1LIKE EMPLOYEE-NAME1,CITYLIKE EMPLOYEE-CITY,END OF ADDR.DATA:ADDRESS TYPE ADDR.MOVE:X TO ADDRESS-FLAG,00001 TO ADDRESS-ID,Smith TO ADDRESS-NAME1,Philadelphia TO ADDRESS-CITY.WRITE ADDRESS.,Declaring a Structure-Method#2,Basic Syntax:TYPES:BEGIN OF,.,.,.,END OF.DATA:TYPE.,Populating a Structure withField-by-Field Transport,REPORT Y170DM37.TABLES:EMPLOYEE.DATA:BEGIN OF ADDRESS,FLAG,IDLIKE EMPLOYEE-ID,NAMELIKE EMPLOYEE-NAME1,CITYLIKE EMPLOYEE-CITY,END OF ADDRESS.SELECT*FROM EMPLOYEE.MOVE-CORRESPONDING EMPLOYEE TO ADDRESS.WRITE:/ADDRESS-FLAG,ADDRESS-ID,ADDRESS-NAME,ADDRESS-CITY.CLEAR ADDRESS.ENDSELECT.,EMPLOYEEAddress,ID Name1 City,000000001 Electronics Inc.Waldorf,MOVE-CORRESPONDING EMPLOYEE TO ADDRESS.,Flag ID Name City,000000001 Waldorf,Clear.,Internal Table Types,StandardSortedHashed,REPORT Y170DM38.TABLES:EMPLOYEE.TYPES:BEGIN OF EMP,IDLIKE EMPLOYEE-ID,NAME1LIKE EMPLOYEE-NAME1,COUNTRYLIKE EMPLOYEE-COUNTRY,END OF EMP.DATA:EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH HEADER LINE.SELECT*FROM EMPLOYEE.MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.APPEND EMPTAB.ENDSELECT.,The TYPES statement defines the structure and data type for the internal table.,The DATA statement with an INITIAL SIZE creates the actual internal table capable of storing data.Because of the WITH HEADER LINE addition,this internal table is created with a header line.,Header Line,ID NAME1 COUNTRY,Creating an Internal Table with Header Line,Internal Table Keys,Implicit KeyAll character fieldsExplicit KeyUser-definede.g.WITH UNIQUE/NON-UNIQUE KEY FIELD1 FIELD2.,Size of an Internal Table,APPEND SORTED BY.,APPEND.,Department Salary,123456,Department Salary,Header,Loading an Internal Table with a Header Line,R&D400,000PROD 7,800,000MKTG 1,000,000SALES 500,000HR 140,000IT 50,000,R&D400,000MKTG 1,000,000SALES 500,000PROD 7,800,000IT 50,000HR 140,000,REPORT Y170DM42.TABLES:EMPLOYEE.TYPES:BEGIN OF EMP,COUNTRYLIKE EMPLOYEE-COUNTRY,IDLIKE EMPLOYEE-ID,SALARYLIKE EMPLOYEE-SALARY,END OF EMP.DATA:EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10 WITH HEADER LINE.SELECT*FROM EMPLOYEE.MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.APPEND EMPTAB.MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.APPEND EMPTAB SORTED BY SALARY.ENDSELECT.,Loading an Internal Table with a Header Line,More than ten entries can be saved in the internal table.,A maximum of ten entries can be saved in the internal table.Any entries that exceed the top ten will be deleted.,With both versions of the APPEND statement,memory space for ten records is allocated when the first record is written to the internal table.,Example 1,Example 2,OR,Internal Table with Header Line,COUNTRY ID FORMA NAME1 SORTL.,ID NAME1COUNTRY,Header Line,A,B,Internal Table with Header Line,ID NAME1 COUNTRY,USA00000001CompanyBaker DistributorsBAKER.,COUNTRY ID FORMA NAME1 SORTL.,Header Line,1,Internal Table with Header Line,Internal Table with Header Line,USA 00000001 Company Baker Distributors BAKER.,ID NAME1 COUNTRY,00000001Baker DistributorsUSA,00000001Baker DistributorsUSA,COUNTRY ID FORMA NAME1 SORTL.,Header Line,.,This header line is attached to the body of the internal table.,Internal Table with Header Line,ID NAME1 COUNTRY,00000001 Baker Distributors USA,00000002 Diversified Indust.USA,00000002 Diversified Indust.USA,USA 00000002 Company Diversified Indust.DIVERS.,EMPLOYEE,COUNTRY ID FORMA NAME1 SORTL.,Header Line,.,Creating an Internal Table without a Header Line,REPORT Y170DM40.TABLES:EMPLOYEE.TYPES:BEGIN OF EMP,IDLIKE EMPLOYEE-ID,NAME1LIKE EMPLOYEE-NAME1,COUNTRYLIKE EMPLOYEE-COUNTRY,END OF EMP.DATA:EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE 10,EMPTAB_WA TYPE EMP.SELECT*FROM EMPLOYEE.MOVE-CORRESPONDING EMPLOYEE TO EMPTAB_WA.APPEND EMPTAB_WA TO EMPTAB.ENDSELECT.,APPEND to.,The TYPES statement defines the structure and data type for the internal table and its work area,Work Area,The DATA statement with an INITIAL SIZE creates the actual internal table without a header line.The DATA statement without the INITIAL SIZE creates the work area for the internal table.,Separate Internal Table Work Area,Performance Issues,Nested Internal Tables,Internal Table wit