SAP ABAP语法实例(二)
- 格式:doc
- 大小:135.75 KB
- 文档页数:11
abap at语法ABAP(Advanced Business Application Programming)是一种在SAP系统中使用的专用编程语言,用于开发和定制企业应用程序。
下面是ABAP语法的一些常见特征:1. ABAP程序以关键词 "REPORT" 或者 "PROGRAM" 开始,并以句点(.)结尾。
例如:REPORT Z_MY_REPORT.或PROGRAM Z_MY_PROGRAM.2. ABAP程序由不同的块组成,包括 "DATA"、"TYPES"、"BEGIN OF"、"LOOP"、"SELECT"、"IF"、"CASE" 等。
例如:DATA: lv_variable TYPE i. "定义变量TYPES: BEGIN OF ty_structure. "定义结构INCLUDE TYPE standard table of sflight. "包含标准表END OF ty_structure.LOOP AT it_table INTO lv_variable. "遍历表...ENDLOOP.SELECT * FROM sflight INTO TABLE it_table. "从数据库中选择数据IF lv_variable > 10. "条件判断...ENDIF.CASE lv_variable. "多重条件判断WHEN 1....WHEN 2....OTHERWISE....ENDCASE.3. ABAP语句以关键词和句点结尾,如 "DATA"、"WRITE"、"IF"、"ENDIF"等。
SAP ABAP语法实例调试1 调试键F8跳过后面所有断点,程序执行完2 外部、会话、调试断点调试断点-debug时打的断点,程序远程完后就会消失会话断点-程序运行前通过会话断点按钮打的断点,在同一登录会话不同窗口都有效,用户注销后消失外部断点-程序运行前通过外部断点按钮打的断点,用户注销后再登录还是有效,但只针对同一用户有效SelectSELECT <result> FROM <source> INTO <target> [WHERE <condition>] [GROUP BY <fields>] [HAVING <cond>] [ORDER BY <fields>].GROUP BY:用于将一组数据条目压缩为一个单行作为选择最终结果;HAVING:用于限定ORDER BY子句子数据目组的选择条件;ORDER BY:用于限定行排序;SELECT整体语法结构。
示例:以上示例查询的是:20170503至20170510期间销售订单为JR01,且销售量大于50的单据的订单号、创建日期、创建者、类型以及合计销量,并按销售订单升序排序。
这里需要强调的是,如果要使用函数(如SUM、MAX、MIN),则需要将其他字段通过GROUP BY 进行分组。
如果需要继续对使用函数进行条件筛选,则可以使用关键字HAVING。
关键字ORDER BY 决定查询结果的排序方法,ASCENDING为升序,DESCENDING为降序。
1 SELECT SINGLE单行数据:SELECT SINGLE <RESULT> INTO <target> FROM <source> ...如果系统找到一个完全符合指定条件的行,SY-SUBRC返回0,否则返回4。
一般不需要指明所有字段,而是在SELECT后用"*"符号, 若指定多个数据对象,则需要将这些对象放在括号中,并用逗号隔开,而且其顺序需要与SELECT子句中指明的表字段顺序一致。
SAP ABAP / 4 基础知识学习数据类型C :字符串D :日期型格式为YYYYMMDD 例:'1999/12/03'F : 浮点数长度为8I :整数N :数值组成的字符串如:011,'302'P : PACKED数用于小数点数值如:12.00542T : 时间格式为:HHMMSS 如:'14:03:00'X : 16进制数如:'1A03'*-------------------------------------------------------------------------------------* 变量声明DATA <F> [<Length>] <type> [<value>][decimals]<F> 变量名称<length><type> 变量类型及长度<value> 初值<decimals> 小数位数exp:DATA : COUNTER TYPE P DECIMALS 3.NAME(10) TYPE C VALUE 'DELTA'.S_DATE TYPE D VALUE '19991203'.exp:DATA : BEGIN OF PERSON,NAME(10) TYPE C,AGE TYPE I,WEIGHT TYPE DECIMALS 2,END OF PERSON.另外,有关DATA声明的指令还有: CONSTANTS(声明常数)、STATICS(临时变量声明). exp:CONSTANTS PI TYPE P DECIMALS 5 VALUE '3.14159'.STATICS 关键字声明的变量仅在目前的程序中使用, 结束后会自动释放语法:STATICS <c> [<length>] <type> [<value>] [<decimals>]系统专用变量说明系统内部专门创建了SYST这个STRUCTURE,里面的栏位存放系统变量,常用的系统变量有: SY-SUBRC: 系统执行某指令后,表示执行成功与否的变量,'0'表示成功SY-UNAME: 当前使用者登入SAP的USERNAME;SY-DATUM: 当前系统日期;SY-UZEIT: 当前系统时间;SY-TCODE: 当前执行程序的Transaction codeSY-INDEX: 当前LOOP循环过的次数SY-TABIX: 当前处理的是internal table 的第几笔SY-TMAXL: Internal table的总笔数SY-SROWS: 屏幕总行数;SY-SCOLS: 屏幕总列数;SY-MANDT: CLIENT NUMBERSY-VLINE: 画竖线SY-ULINE: 画横线TYPE 关键字用来指定资料型态或声明自定资料型态Example:TYPES: BEGIN OF MYLIST,NAME(10) TYPE C,NUMBER TYPE I,END OF MYLIST.DATA LIST TYPE MYLIST.LIKE 关键字跟TYPE关键字使用格式相同, 如DATA TRANSCODE LIKE SY-TCODE.不同的是LIKE 用在已有值的资料项, 如系统变量, 而TYPE关键字则是用在指定资料型态。
SELECTBasic formSELECT select clause [INTO clause] FROM from clause [WHERE cond1] [GROUP BY fields1] [HAVING cond2] [ORDER BY fields2].EffectReads a selection and/or a summary of data from one or more database tables and/or views (see relational database). SELECT is an OPEN SQL statement.Each SELECT statement consists of a series of clauses, each with a differen task:The SELECT clause select clause describes∙Whether the result of the selection should be a single record or a table,∙Which columns should be contained in the result,∙Whether identical lines may occur in the result.The INTO clause INTO clause determines the target area into which the selected data is read. If the target area is an internal table, the INTO clause specifies:∙Whether you want to overwrite the contents of the internal table or∙Append the results to the internal table, and∙Whether you want to place the data in the internal table in a single step, or in a series of packages.The INTO clause can also occur after the FROM clause. You may omit it if∙The SELECT clause contains a "*",∙The FROM clause does not contain a JOIN, and∙You have declared a table work area dbtab in your program using TABLES.The data, if it exists in the database, is then made available using the table work area dbtab. The statement is then processed further like the SELECT * INTO dbtab FROM dbtab statement, which has the same effect.If the result of the selection is a table, the data is normally read line by line (for further information, see INTO clause) in a processing loop, which is introduced with SELECT and concludes with ENDSELECT. The loop is processed once for each line that is read. If you want the result of the selection to be a single record, there is no concluding ENDSELECT statement.The FROM clause FROM clause specifies the source of the data (database tables or views), from which you want to select the data. It also specifies the∙Client handling,∙Behavior for buffered tables, and∙The maximum number of lines that you want to read.The WHERE clause cond1 specifies the conditions that the result of the selection must satisfy. By default, only data from the current client is selected (without you having to specify the client field specifically in the WHERE clause). If you want to select data from several clients, you must use the ... CLIENT SPECIFIED addition in the FROM clause.The GROUP BY clause fields1 combines groups of lines into single lines of the result table.A group is a set of records with the same value of each database field listed in the GROUP BY clause.The HAVING clause cond2 specifies conditions for the combined lines of the result table.The ORDER BY clause fields2 specifies how the records in the result table should be arranged.The system field SY-DBCNT contains the number of lines read so far ecah time the SELECT statement is executed. After ENDSELECT, SY-DBCNT contains the total number of records read.The return code is set as follows:SY-SUBRC = 0:The result table contains at least one record.SY-SUBRC = 4:The result table is empty.SY-SUBRC = 8:Applies only to SELECT SINGLE FOR UPDATE: You did not specify all of the primary key fields in the WHERE condition. The result table is empty.NoteThe SELECT COUNT( * ) FROM ... statement returns a result table containing a single line with the result 0 if there are no records in the database table that meet the selection criteria. In an exception to the above rule, SY-SUBRC is set to 4 in this case, and SY-DBCNTto zero.ExampleDisplaying the passenger list for Lufthansa flight 0400 on 2/28/1995:DATA: WA_SBOOK TYPE SBOOK.SELECT * FROM SBOOK INTO WA_SBOOKWHERECARRID = 'LH ' ANDCONNID = '0400' ANDFLDATE = '19950228'ORDER BY PRIMARY KEY.WRITE: / WA_SBOOK-BOOKID, WA_SBOOK-CUSTOMID,WA_SBOOK-CUSTTYPE, WA_SBOOK-SMOKER,WA_SBOOK-LUGGWEIGHT, WA_SBOOK-WUNIT,WA_SBOOK-INVOICE.ENDSELECT.NotePerformance:Storing database tables in a local buffer (see SAP buffering) can lead to considerable time savings in a client/server environment, since the access time across the network is considerably higher than that required to access a locally-buffered table.Notes1. A SELECT statement on a table for which SAP buffering has been declared in theABAP Dictionary usually reads data from the SAP buffer without accessing thedatabase. This does not apply when you use:- SELECT SINGLE FOR UPDATE or- SELECT DISTINCT in the SELECT clause,- BYPASSING BUFFER in the FROM clause,- ORDER BY f1 ... fn in the ORDER BY clause,- Aggregate functions in the SELECT clause,- When you use IS [NOT] NULL in the WHERE condition,or when the table has generic buffering and the appropriate section of the key is not specified in the WHERE condition.2. The SELECT statement does not perform its own authorization checks. You shouldwrite your own at program level.3. Proper synchronization of simultaneous access by several users to the same set ofdata cannot be assured by the database lock mechanism. In many cases, you will need to use the SAP locking mechanism.4. Changes to data in the database are not made permanent until a database commit(see LUW) occurs. Up to this point, you can undo any changes using a databserollback (see Programming Transactions). At the lowest isolation level (see lock mechanism ), the "Uncommitted Read", it can sometimes be the case that dataselected by a SELECT statement was never written to the database. While a program is selecting data, a second program could be adding data to, changing data in, or deleting data from the database at the same time. If the second program thenexecutes a rollback, the first program has selected a set of data that may onlyrepresent a temporary state from the database. If this kind of "phantom data" isunacceptable in the context of your application, you must either use the SAP locking mechanism or change the isolation level of the database system to at least"Committed Read" (see locking mechanism).5. In a SELECT - ENDSELECT loop, the CONTINUE statement terminates the currentloop pass and starts the next.6. If a SELECT - ENDSELECT loop contains a statement that triggers a databasecommit, the cursor belonging to the loop is lost and a program termination andruntime error occur. Remote Function Calls and changes of screen always lead to adatabase commit. The following statements are consequently not allowed wihtin aSELECT-ENDSELECT loop: CALL FUNCTION ... STARTING NEW TASK, CALLFUNCTION ... DESTINATION, CALL FUNCTION ... IN BACKGROUND TASK,CALL SCREEN, CALL DIALOG, CALL TRANSACTION, and MESSAGE.7. On some database systems (for example DB2/390)locking conflicts can be caused even by read access. You can prevent this problemfrom occurring using regular database commits.SELECT ClauseVariants:1. SELECT [SINGLE [FOR UPDATE] | DISTINCT] *2. SELECT [SINGLE [FOR UPDATE] | DISTINCT] s1 ... sn3. SELECT [SINGLE [FOR UPDATE] | DISTINCT] (itab)EffectThe result of a SELECT statement is itself a table. The SELECT clause, along with the database tables and/or views in the FROM clause, specifies the sequence, name, database type, and length of the columns of the result table.You can also use the optional additions SINGLE or DISTINCT to indicate that only certain lines in the result set should be visible to the program:SINGLEThe result of the selection should be a single entry. If it is not possible to identify a unique entry, the system uses the first line of the selection. If you use the FOR UPDATE addition, the selected entry is protected against parallel updates from other transactions until the next database commit (see LUW and database lock mechanism). If the database system identifies a deadlock, a runtime error occurs. DISTINCTDuplicate entries in the result set are automatically deleted.NoteTo ensure that an entry can be uniquely identified, you can specify all of the fields in the primary key using AND expressions in the WHERE condition.NotePerformance:1. The additions SINGLE FOR UPDATE and DISTINCT bypass the SAP buffering.2. The addition DISTINCT forces a sort on the database server. You should thereforeonly use it if you are really expecting duplicates in the result set.Variant 1SELECT [SINGLE [FOR UPDATE] | DISTINCT] *EffectThe columns of the result set will have exactly the same sequence, names, database type, and length as the fields of the database table or view specified in the FROM clause.ExamplesExample to display all flights from Frankfurt to New York:DATA WA_SPFLI TYPE SPFLI.SELECT * FROM SPFLI INTO WA_SPFLIWHERECITYFROM = 'FRANKFURT' ANDCITYTO = 'NEW YORK'.WRITE: / WA_SPFLI-CARRID, WA_SPFLI-CONNID.ENDSELECT.Example to display the free seats on Lufthansa flight 0400 on 02.28.1995:DATA WA_SFLIGHT TYPE SFLIGHT.DATA SEATSFREE TYPE I.SELECT SINGLE *FROM SFLIGHT INTO WA_SFLIGHTWHERECARRID = 'LH ' ANDCONNID = '0400' ANDFLDATE = '19950228'.SEATSFREE = WA_SFLIGHT-SEATSMAX - WA_SFLIGHT-SEATSOCC.WRITE: / WA_SFLIGHT-CARRID, WA_SFLIGHT-CONNID,WA_SFLIGHT-FLDATE, SEATSFREE.NoteIf you specify more than one table in the FROM clause and the INTO clause contains an internal table or work area instead of a field list, the fields are placed into the target area from left to right in the order in which they occur in the tables in the FROM clause. Gaps may occur between the table work areas for the sake of alignment. For this reason, you should define the target work area by referring to the types of database tables instead of simply listing the fields. For an example, refer to the documentation of the FROM clause.Variant 2SELECT [SINGLE [FOR UPDATE] | DISTINCT] s1 ... snEffectThe columns of the result table will have the same format as the column references s1 ... sn.If si stands for a field f, MAX( f ), MIN( f ), or SUM( f ), the corresponding column in the result set will have the same ABAP Dictionary format as f. For COUNT( f ) orCOUNT( * ) the column has the type INT4. For AVG( f ) it has the type FLTP.If you use aggregate functions with one or more database fields in the SELECT clause, you must include all of the database fields that are not used in the aggregate function in the GROUP BY clause. The result of the selection in this case is a table.If the SELECT clause only contains aggregate functions, the result of the selection will be a single entry. In this case, SELECT does not have a corresponding ENDSELECT statement.Notes1. You can only use this variant for pool and cluster tables if the SELECT clause doesnot contain any aggregate functions.2. As a rule, aggregate functions used together with the FOR ALL ENTRIES addition donot return the desired values. The result is only correct if the fields in the WHEREcondition that are linked with AND and tested for equality with the aggregated fieldscan identify the table line uniquely.3. If you use a database field with type LCHAR or LRAW in the SELECT clause, you mustspecify the corresponding length field immediately before it in the SELECT clause. NotesPerformance:1. When you use aggregate functions, the system bypasses the SAP buffer.2. Since not all database systems can store the number of lines in a table in theircatalog, and therefore retrieving them is time-consuming, the COUNT( * ) functionis not suitable for testing whether a table contains any entries at all. Instead, youshould use SELECT SINGLE f ... for any table field f.3. If you only want to select certain columns of a database table, use a field list in theSELECT clause or a view.ExamplesExample to display all destinations served by Lufthansa from Frankfurt:TABLES SPFLI.DATA TARGET LIKE SPFLI-CITYTO.SELECT DISTINCT CITYTOINTO TARGET FROM SPFLIWHERECARRID = 'LH ' ANDCITYFROM = 'FRANKFURT'.WRITE: / TARGET.ENDSELECT.Example to display the number of airlines that fly to New York:TABLES SPFLI.DATA COUNT TYPE I.SELECT COUNT( DISTINCT CARRID )INTO COUNTFROM SPFLIWHERECITYTO = 'NEW YORK'.WRITE: / COUNT.Example to find the number of passengers, the total luggage weight, and the average weight of the luggage for all Lufthansa flights on 02.28.1995:TABLES SBOOK.DATA: COUNT TYPE I, SUM TYPE P DECIMALS 2, AVG TYPE F.DATA: CONNID LIKE SBOOK-CONNID.SELECT CONNID COUNT( * ) SUM( LUGGWEIGHT ) AVG( LUGGWEIGHT ) INTO (CONNID, COUNT, SUM, AVG)FROM SBOOKWHERECARRID = 'LH ' ANDFLDATE = '19950228'GROUP BY CONNID.WRITE: / CONNID, COUNT, SUM, AVG.ENDSELECT.Variant 3SELECT [SINGLE [FOR UPDATE] | DISTINCT] (itab)EffectWorks like SELECT [SINGLE [FOR UPDATE] | DISTINCT] s1 ... sn, if the internal table itab contains the list s1 ... sn as ABAP source code, and works like SELECT [SINGLE [FOR UPDATE] | DISTINCT] *, if itab is empty. The internal table itab may only contain one field, which must have type C and not be longer than 72 characters. Youmust specify itab in parentheses. Do not include spaces between the parentheses and the table name.NoteThe same restrictions apply to this variant as to SELECT [SINGLE [FOR UPDATE] | DISTINCT] s1 ... sn.ExampleExample to display all Lufthansa routes:DATA WA_SPFLI TYPE SPFLI,WA_FTAB(72) TYPE C, FTAB LIKE TABLE OF WA_FTAB.CLEAR FTAB. FTAB = 'CITYFROM'. APPEND WA_FTAB TO FTAB. FTAB = 'CITYTO'. APPEND WA_FTAB TO FTAB. SELECT DISTINCT (FTAB) INTO CORRESPONDING FIELDS OF WA_SPFLIFROM SPFLIWHERECARRID = 'LH'. WRITE: / WA_SPFLI-CITYFROM, WA_SPFLI-CITYTO. ENDSELECT.。
SAP ABAP(Advanced Business Application Programming)是一种面向SAP系统的编程语言,用于开发SAP应用程序。
下面是SAP ABAP语法的一些基本元素和特点:
1. **变量声明**:使用“DATA”关键字声明变量,例如:DATA: counter TYPE i.
2. **数据类型**:ABAP支持多种数据类型,包括数值型(如i, p, f, decimals等)、字符型(如c, string等)、布尔型、表类型等。
3. **控制结构**:ABAP具有条件语句(IF...ENDIF)、循环语句(LOOP...ENDLOOP)和选择语句(CASE...ENDCASE)等控制结构。
4. **程序注释**:使用“*”作为注释的开头,并写在第一列,例如:* This is a comment.
5. **函数和过程**:ABAP支持自定义函数和过程,使用“FUNCTION”或“METHOD”关键字声明,并在其内部编写代码。
6. **事务代码**:用于与SAP系统进行交互的代码块,例如:SE80用于创建新对象,SE11用于显示物料主数据等。
7. **数据字典**:用于定义数据元素、数据表、视图和结构等。
8. **屏幕处理**:ABAP支持屏幕元素的读取、写入和修改,以便与用户进行交互。
9. **事务处理**:ABAP可以处理事务代码的执行,包括事务
的开始、执行和结束。
10. **系统参数**:通过系统参数设置来配置ABAP应用程序的行为。
这些是SAP ABAP语法的一些基本元素和特点。
要深入了解SAP ABAP语法的更多细节,建议参考SAP官方文档或相关的ABAP 编程书籍。
sap abap read table 新语法在SAP ABAP中,READ T ABLE语句用于从数据表中读取数据。
新的语法结构使读取数据变得更加灵活和高效。
在新的语法中,READ T ABLE语句使用了关键字SELECT和INTO来指定需要读取的数据表和将数据存储到的变量。
语法结构如下:```sqlREAD T ABLE itab WITH KEY index1 = value1 index2 = value2 ... SELECT column1 column2 ... INTO (var1 var2 ...) FROM table.```其中,itab是数据表的引用变量,可以使用关键字TUPLE来指定数据表中的每一行。
index1、index2等是数据表中的索引列,value1、value2等是要匹配的值。
SELECT后面是要读取的列名,INTO后面是要将数据存储到的变量名,FROM后面是要读取的数据表名。
例如,假设有一个名为"employees"的数据表,其中包含员工的姓名、年龄和工资等信息。
要读取年龄为30岁的员工姓名和工资,可以使用以下代码:```sqlDA T A: itab TYPE T ABLE OF employees WITH HEADER LINE.wa_emp TYPE employees.wa_emp_result TYPE employees.INDEXES: i_age TYPE i.wa_age TYPE i.wa_salary TYPE f.wa_age = 30.READ T ABLE itab WITH KEY age = wa_age SELECT name salary INTO (wa_emp_result-name wa_emp_result-salary) FROM employees INDEX i_age.```在这个例子中,首先定义了一个名为"itab"的数据表引用变量,然后定义了要读取的变量"wa_emp_result-name"和"wa_emp_result-salary",并将它们存储到对应的变量中。
第二章ABAP/4 程序语法和格式概览内容语法元素............................................................................................................................. 1语句............................................................................................................................... 1关键字 ........................................................................................................................... 2注释............................................................................................................................... 2语法结构............................................................................................................................. 3语句结构........................................................................................................................ 4注释结构........................................................................................................................ 4连接相似语句............................................................................................................... 5ABAP/4 程序格式.............................................................................................................. 6缩排语句块.................................................................................................................... 6使用模块化工具........................................................................................................... 6正确插入程序注释 ....................................................................................................... 6整齐打印程序............................................................................................................... 6插入已有结构.................................................................................................................... 7插入已有关键字结构.................................................................................................... 7插入已有注释行........................................................................................................... 8本节说明ABAP/4 语法,并且提供关于如何在ABAP/4 中编程的建议。
SAP系统中ABAP程序运行示例•上图说明了在各个服务层之间的数据交互,需要说明的是,实际处理时应用层与数据库的交互比上图中更为频繁,不仅仅在处理用户操作时才读写数据库。
后面的例子会详细说明这一过程。
•当用户触发一个操作,例如按了回车、功能键、菜单、按钮等时,就会把控制权从表示层传递给应用层。
•只有跟用户操作相关的那部分程序会被执行。
•如果在程序中触发了其他会话,系统会把屏幕传递给表示层,同时交出控制权。
1、程序开始–用户登录到系统之后,就会出现初始屏幕,在初始屏幕里,可以通过菜单路径或事务码来启动ABAP 程序。
–知识库(Repository),它是SAP 系统中非常重要的组成部分,负责存储系统运行的基本数据,例如:程序库、数据字典、ABAP 对象等,被保存在数据库中,后面会专题讲解它。
2、系统读取程序上下文–上下文(Context),在汉语里面还真没什么合适的词可以跟这个英文单词对应的,只可意会不可言传。
–系统在程序开始后,会把所有程序上下文读到应用服务器上。
程序上下文包括:存储变量和复杂对象的内存区,用户会话的屏幕信息,ABAP程序块等。
上述的这些东西都存储在知识库中。
–上页的例子里,用到了一个选择屏幕与用户交互,一个变量和一个结构,还有一个ABAP程序块。
显示数据的清单则根据用户输入动态生成。
–ABAP 运行时环境控制后面的程序流。
3、运行时系统发送选择屏幕–如果运行时系统包含了选择屏幕,ABAP运行时系统就会把屏幕发送给表示层服务器,在用户没有输入完成前,表示层服务器就控制着程序流。
–选择屏幕可以做一些基础的输入有效性检查,如必输字段、日期/数值格式等。
4、输入的值被插入数据对象–用户完成了在选择屏幕的输入后,就可以通过触发执行来处理程序。
–输入的数据自动被保存到程序相应的数据对象中,ABAP 运行时系统又重新获得程序的控制权。
–在我们的示例中,只有一个程序块,运行时系统就顺序处理ABAP程序块。
SAPABAP基本语法介绍ABAP(Advanced Business Application Programming)是SAP (System, Applications, and Products in Data Processing)系统最常用的编程语言之一,主要用于SAP软件开发和定制。
ABAP具有强大的功能和灵活性,可用于创建和维护企业应用程序,包括各种业务流程和数据处理。
在本文中,我将介绍ABAP的基本语法和一些常见的编程概念。
1.ABAP程序结构ABAP程序由多个模块组成,每个模块都是独立的功能单元。
一个ABAP程序通常由一个开始模块和若干子模块组成。
开始模块包含程序的整体逻辑,而子模块用于实现程序的具体功能。
2.声明变量在ABAP中,变量需要先声明后才能使用。
变量可以是内部表、字段符号、工作区、宏定义等。
声明变量时需要指定数据类型和名称。
例如:DATA: lv_name TYPE string.3.控制语句ABAP支持多种控制语句,用于实现条件判断、循环和跳转等功能。
常见的控制语句包括IF语句、CASE语句、DO循环和WHILE循环等。
例如:WRITE: 'Number is 1'.WRITE: 'Number is 2'.ELSE.WRITE: 'Number is neither 1 nor 2'.ENDIF.4.数据操作ABAP提供了丰富的数据操作函数,可用于对数据进行处理和转换。
例如,可以使用CONCATENATE函数将多个字符串连接在一起,使用SUBSTRING函数获取字符串的子串,使用REPLACE函数替换字符串中的内容等。
5.内部表ABAP中的内部表类似于其他编程语言中的数组或列表,用于存储和处理数据。
内部表可以是标准表、排序表、哈希表等类型。
可以使用内部表来读取、修改和删除数据。
例如:SELECT * FROM kna1 INTO TABLE lt_customers WHERE land1 ='US'.6.函数模块和方法ABAP中的函数模块类似于其他编程语言中的函数或方法。
SAP ABAP语法实例调试1 调试键F8跳过后面所有断点,程序执行完2 外部、会话、调试断点调试断点-debug时打的断点,程序远程完后就会消失会话断点-程序运行前通过会话断点按钮打的断点,在同一登录会话不同窗口都有效,用户注销后消失外部断点-程序运行前通过外部断点按钮打的断点,用户注销后再登录还是有效,但只针对同一用户有效SelectSELECT <result> FROM <source> INTO <target> [WHERE <condition>] [GROUP BY <fields>] [HAVING <cond>] [ORDER BY <fields>].GROUP BY:用于将一组数据条目压缩为一个单行作为选择最终结果;HAVING:用于限定ORDER BY子句子数据目组的选择条件;ORDER BY:用于限定行排序;SELECT整体语法结构。
示例:以上示例查询的是:20170503至20170510期间销售订单为JR01,且销售量大于50的单据的订单号、创建日期、创建者、类型以及合计销量,并按销售订单升序排序。
这里需要强调的是,如果要使用函数(如SUM、MAX、MIN),则需要将其他字段通过GROUP BY 进行分组。
如果需要继续对使用函数进行条件筛选,则可以使用关键字HAVING。
关键字ORDER BY 决定查询结果的排序方法,ASCENDING为升序,DESCENDING为降序。
1 SELECT SINGLE单行数据:SELECT SINGLE <RESULT> INTO <target> FROM <source> ...如果系统找到一个完全符合指定条件的行,SY-SUBRC返回0,否则返回4。
一般不需要指明所有字段,而是在SELECT后用"*"符号, 若指定多个数据对象,则需要将这些对象放在括号中,并用逗号隔开,而且其顺序需要与SELECT子句中指明的表字段顺序一致。
这里的<target>为结构非内表。
示例:TABLES: sflight.DATA: sum TYPE i VALUE 0.SELECT SINGLE SUM( seatsocc ) INTO sum FROM sflight WHERE carrid ='LH'. WRITE:/ sum.以上实现的是航线为LH的合计座位数。
区别以下代码,上面代码只输出一行,下面则输出多行:2 SELECT / ENDSELECT循环通过SELECT / ENDSELECT循环从数据库中读取多行。
SELECT [DISTINCT] <result>...<statement block> ENDSELECT.注:使用DISTINCT自动去掉重复的行;SY-DBCNT(系统字段)为每次循环计数.示例:使用distinct达到的效果与select single一样,建议使用select single语句。
3 一次性SELECT ...INTO一次性把数据选择到一个内表中去。
SELECT ...INTO|APPENDING[CORRESPONDING FIELDS OF]TABLE itab.该情况下SELECT并不启动循环,因而不需要使用ENDSELECT语句;如果itab非空,则SELECT语句将用读取的数据覆盖其中的内容,使用APPENDING代替INTO将不覆盖内表,若结构不完全相同,也可使用CORRESPONDING FIELDS选项将同名区域相对应。
SQL编写注意事项-性能实例1直接内表操作优先尽量不要在频率较高的循环语句中使用update\insert\delete\modify等操作,即尽量不要通过工作区实现,直接通过操作内表实现。
示例:2 select …into …table优于select…append table..endselect在对内表赋值时,尽量使用select …into …table的写法来代替select…append table..endselect 的写法。
尽量用对内表的操作来代替对数据库的操作。
示例:DATA: BEGIN OF it_mara OCCURS 0,matnr LIKE mara-matnr,maktx LIKE makt-maktx,END OF it_mara.第一种写法:Select matnrINTO it_maraFROM mara.APPEND it_mara.ENDSelect.第二种写法(high performace):Select matnrINTO TABLE it_maraFROM mara.3 使用for all entries不推荐Loop at int_cntry.Select single * from zfligh into int_fligh where cntry = int_cntry-cntry. Append int_fligh.Endloop.推荐Select * from zfligh appending table int_flighFor all entries in int_cntryWhere cntry = int_cntry-cntry.内表可以用来存放多笔数据,OPEN SQL允许以内表数据作为查询条件,以方便对查询对查询数据的进一步的筛选,相关语法如下:Select <f1…fn> from <dbtab> FOR ALL ENTRIED IN <itab> WHERE …需要注意的是,在FOR ALL ENTRIED IN itab 之前,一定要检查itab表是否为空,若为空则不执行查询,否则会查询所有非条件限制的数据。
示例1:CHECK LT_MKPF[] IS NOT INITIAL.* IF P_MATNR[] IS NOT INITIAL.SELECT MBLNR ZEILE MJAHR BWART WERKS MATNR MENGE SHKZG FROM MSEG INTO CORRESPONDING FIELDS OF TABLE LT_MSEGFOR ALL ENTRIES IN LT_MKPFWHERE MBLNR = LT_MKPF-MBLNRand MJAHR = LT_MKPF-MJAHRand WERKS IN P_WERKSAND MATNR IN P_MATNRAND BWART IN P_BWART.示例2:DATA: BEGIN OF it_mara OCCURS 0,matnr LIKE mara-matnr,maktx LIKE makt-maktx,END OF it_mara.DATA: BEGIN OF it_makt OCCURS 0,matnr LIKE mara-matnr,maktx LIKE makt-maktx,END OF it_makt.第一种写法:(需定义it_mara)Select matnr INTO TABLE it_mara FROM mara.LOOP AT it_mara.Select SINGLE maktx INTO it_mara-maktx FROM maktWhere matnr = it_mara-matnr AND spras = sy-langu.MODIFY it_mara TRANSPORTING maktx. *只修改maktx字段ENDLOOP.第二种写法(需定义it_mara &it_makt): ——high performaceSelect matnr INTO TABLE it_mara FROM mara.Select matnr maktx INTO TABLE it_makt FROM maktFOR ALL ENTRIES IN it_maraWhere matnr = it_mara-matnr and spras = sy-langu.4 where…in…对存在OR条件等判断的语句,尽量使用IN来代替。
5 where条件●Where 语句中尽量避免使用”<” ”>”等模糊条件来查询。
●不要使用CHECK语句对table 进行条件查询,用where语句代替。
6 Join语句连接多个表数据查询尽量使用JOIN语句,且注意应选择数据量最小的表来作为基表,尽量避免三个以上的表的相关JOIN查询。
7 select single在查询单条语句时,尽量使用select single语句,不要使用select…endselect语句。
8 Up to n rows使用语法up to n rows来实现对数据前n项的查询。
9 Select 栏位尽量具体尽量使用select f1 f2…具体栏位来代替select * 写法。
10利用函数充分利用系统提供的标准函数,如max,min,avg,sum,count.示例(使用max聚合函数):不推荐Maxnu = 0.Select * from zflight where airln = ‘LF’ and cntry = ‘IN’.Check zflight-fligh > maxnu.Maxnu = zflight-fligh.Endselect.推荐Select max( fligh ) from zflight into maxnu where airln = ‘LF’ and cntry = ‘IN’. 11使用INTO table 代替select endselect不推荐Refresh: int_fligh.Select * from zflight into int_fligh.Append int_fligh. Clear int_fligh.Endselect.推荐Refresh: int_fligh.Select * from zflight into table int_fligh.TABLE1 RANGE TABLERange Table 为SAP R/3系统标准内表的一种,结构与Selection Table 一致,由SIGN, OPTION, LOW 和HIGH字段组成;可以通过TYPE RANGE OF 语句或RANGES 关键字定义Range T able。
1) TYPE RANGE OF…DATA rtab {TYPE RANGE OF type}|{LIKE RANGE OF dobj}[INITIAL SIZE n][WITH HEADER LINE][VALUE IS INITIAL][READ-ONLY].示例:2) RANGES语法:RANGES rtab FOR dobj [OCCURS n].示例:2 两个内表间赋值●Itab1[]=itab2[]示例:tables: zcustom,scustom.data t1_zcustom type STANDARD TABLE OF zcustom with HEADER LINE.data t2_zcustom like STANDARD TABLE OF zcustom WITH HEADER LINE.select * from scustom into CORRESPONDING FIELDS OF TABLE t1_zcustom.t2_zcustom[] = t1_zcustom[].●APPEND lines of itab2 to itab1(append itab2 to itab1)Itab2 to itab1 还可通过LOOP 语句循环读取内表实现。