ABAP4语法集锦(中文版).doc_第1页
ABAP4语法集锦(中文版).doc_第2页
ABAP4语法集锦(中文版).doc_第3页
ABAP4语法集锦(中文版).doc_第4页
ABAP4语法集锦(中文版).doc_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

abap/4 training. abap/4 data element一. data type (数据类型)c: 字符(串), 长度为1, 最大有65535 bytes, 初始值为: space,例: m;d: 日期, 格式为yyyymmdd, 最大是9999/12/31 ,例:1999/12/03.f: 浮点数, 长度为8, 例如: 4.285714285714286e-01i: 整数 范围 :-231 231-1n: 数值组成的字符串: 011, 302.p: packed 数,用于小数点数值,例如: 12.00542;t: 时间, 格式为hhmmss,例如: 14:03:00, 21:30:39.x: 16进制数, 例如 1a03.二. 变量宣告变量宣告包含name, length, type, structure等,语法如下: data 其中: :变量名称,最长30个字符,不可含有 + , . , : ( ) 等字符; :变量类型及长度; :初值 :小数位数example 1: data: counter type p decimals 3, name (10) type c value delta, s_date type d value 19991203.example 2: data: begin of person, name(10) type c, age type i, weight type p decimals 2, end of person.另外,有关data宣告的指令还有: constants(宣告常数)、statics(临时变量宣告).三. 系统专用变量说明系统内部专门创建了syst这个structure,里面的字段存放系统变量,常用的系统变量有: sy-subrc : 系统执行某指令后,表示执行成功与否的变量,0 表示成功 sy-uname: 当前使用者登入sap的username; sy-datum: 当前系统日期; sy-uzeit: 当前系统时间; sy-tcode: 当前执行程序的transaction code sy-index : 当前loop循环过的次数sy-tabix: 当前处理的是internal table 的第几笔sy-tmaxl: internal table的总笔数sy-srows: 屏幕总行数;sy-scols: 屏幕总列数;sy-mandt: client number sy-vline: 画竖线 sy-uline: 画横线附注:1. sap的全称是: system application products in data processing;2. abap/4的全称是:advanced business application programming;3. abap/4的路径为:tools abap/4 workbenchabpa/4 editor ;4. abap/4每条语句以句号结束;5. abap/4中象= , ,+,-,*,/等符号左右都需要有至少一个空格;6. 整行注释用*号, 注释本行后面部分用”号;outputting data to screen一. write 语句abap/4用来在屏幕上输出数据的指令是write指令,例如: write: user name is:, sy-uname.二. 指定屏幕输出位置指定输出位置的语句格式为: write: at / () 资料项 其中: / : 在下一行输出: 指定输出的行号;():指定输出位数(长度): 指定显示格式参数,参数有: left-justified 资料靠左对齐 centered 数据靠中间对齐 right-justified 资料靠右对齐 under 正对在数据项的下面显示 no-gap 紧接着显示,不留空格 using edit mask : 使用内嵌子元显示, 如 12:03:20 using no edit mask: 不使用内嵌子元 no-zero: 数字前面 0 的部分不显示 no-sign: 不显示正负号 decimals : 显示 位小数 expoent : f(浮点数)指数的值 round : 四舍五入至小数点后位 currency : 币别显示 dd/mm/yy : 日期显示格式 mm/dd/yy: yy/mm/dd: yy/dd/mm mm/dd/yyyy: dd/mm/yyyy yyyy/mm/dd: yyyy/dd/mm:例如1: write: /10(6) abcdefghijk.输出结果为: abcdef例如2: data: x type i value 11:20:30, a(5) type c value ab cde. write: / x using edit mask _:_:_. write: / x using edit mask $_,_. write: / y no-gap.输出结果为: 11:20:30 $112,030 abcdef四. 显示图标:语法: write: as symbol. write: as icon.例如: include . include .write: / phone symbol:, sym_phone as symbol.write: / alarm icon:, icon_voice_output as icon.要查看系统所提供有那些符号及图标,可选择edit下的insert statement,选择write,接下来选择要查看的群组,如symbol 或icon, 接下来按display即可. internal table一. internal table 的宣告abap/4中的internal table是一种data structure,类似于其它语言中的struture,它可以由几个不同类型的字段(field)组成,用来表示具有不同属性的某一事物,单独一笔资料表示某个事物,多笔数据表示具有相同属性的多个事物.例如:为了存取或记录某班的同学数据,我们创建如下的internal table:data: begin of student occurs 20, std_id type n, name(10) type c, age type i, birth type d, score type p decimals 2, end of student.此时我们已经创建了名叫student的internal table,并且为它预先申请了能够存放20笔数据的buffer(当然,如果存取数据不止20笔,程序执行时,会自动申请系统buffer)internal table 的定义有以下几种格式:格式一. data: begin of occurs , type , type , type , end of .格式二. types: begin of , type , type , type , end of . types type occurs .格式三. data: begin of . include structure . data: end of . data: like occurs .二. append line格式: append to .举例一. (使用work area)data: begin of line, col1 type i, col2 type i, end of line.data itab like line occurs 10.do 2 times. line-col1 = sy-index. line-col2 = sy-index * 2. append line to itab.enddo.loop at itab into line. write: / line-col1, line-col2.endloop.执行结果为:1 12 4举例二. (不使用work area) data: begin of itab occurs 10, col1 type i, col2 type i, end of itab. do 2 times. itab-col1 = sy-index. itab-col2 = sy-index * 2. append itab. enddo. loop at itab. write: / itab-col1, itab-col2. endloop. 执行结果与举例一相同.举例三. (加入另一个internal table的元素) 格式: append lines of from to to . 将的元素加入至中,可选取自至的范围. append lines of itab to jtab.三. collect linecollect 指令也是将元素加入internal table中,与append 的区别是: collect指令在非数值字段相同的情况下,将数值字段汇总.格式: collect into data: begin of itab occurs 3, col1(3) type c, col2 type i, end of itab. itab-col1 = abc. itab-col2 = 10. collect itab. itab-col1 = xyz. itab-col2 = 20. collect itab. itab-col1 = abc. itab-col2 = 80. collect itab.此时, internal table中放的是2笔数据, 分别为: itab-col1 itab-col2 abc 90 xyz 20四. insert line将元素插入在指定的internal table位置之前.格式: insert into initial line into index 或者: insert lines of from to into index 其中: 即work area,工作区中的元素.initial line into :插入一笔初始化的记录.: internal tableindex : internal table 的记录号.(新加入的元素放在此记录前面)五. 读取internal table格式一: loop at into from to where endloop.格式二:read table into index / with key 举例. (格式二)data: begin of itab occurs 10, col1 type i, col2 type i, end of itab. do 10 times. itab-col1 = sy-index. itab-col2 = sy-index * 2. append itab.enddo.read table itab index 3.(或者: read table itab with key col1 = 3.)write: / itab-col1 = , itab-col1, itab-col2 = , itab-col2.执行结果同样是: itab-col1 = 3itab-col2 = 6.六. 修改internal table 中的值格式: modify from index transporting where 举例一. read table itab index 3. line-col1 = 29. modify itab from line transporting col1. 将第三笔记录的col1字段的值修改为29.举例二. t_salary salary = 50. modify t_salary transporting salary where birthday = 1999/12/06.七. delete internal table中的字段格式: delete index .或: delete from to where 八. internal table 排序 sort by 其中: 有descending 和ascending, default 为ascending. : 为指定排序的字段.九. 加总sum.总和计算存放与work area中,但只能在loop 中使用.例: loop at itab into line. sum. endloop. write: / line-col1, line-col2.十. 初始化internal tablerefresh . 清空中的值.clear . 清空的header line.free . 释放记忆体空间.屏幕输入命令 在abap/4中要从屏幕输入变量, 使用的命令是 parameters 及selection-options: 1. parameter: 输入一个变量 2. selection-options: 使用条件筛选画面来输入数据 一. parameters 指令基本的输入命令, 类似如basic的input命令, 但无法使用f格式(浮点数) 语法: parameters default lower case obligatory as checkbox radiobutton group example: parameters: name(8), age type i, birth type d. 执行结果: 在日期的输入格式上为 mm/dd/yy , mm/dd/yyyy, mmddyy或mmddyyyy , 如输入 020165表 1965年02月01日, 与02/01/65的输入是一样的, 日期输入范围为公元1950年至2049年1. default 设定输入的默认值example: parameters: company(20) default delta, birth type d default 19650201.2. lower case abap/4预设是将字符串输入值自动转换为大写, 加上此参数会将输入的数据转成小写, 3. obligatory强制要求输入, 屏幕上会出现一个 ? , 使用者必须要输入才可.4. as checkbox 输入 checkbox的格式 example: parameters: tax as checkbox default x, ntd as checkbox. 执行结果: 5. radiobutton group 输入 radio button group 的方式 example: parameters: boy radiobutton group sex default x, girl radiobutton group sex. 执行结果:二. select-options selection-options所输入的值实际上是放在internal table中的,该internal table 有四个字段,分别是:sign,option,low,high. 条件筛选检查条件输入画面指令, 输入条件后可配合select指令自table读取符合条件的数据, 直接执行或放入 internal table中, 条件有四个参数:1. sign: i: 表筛选条件符合的资料 e: 表筛选条件不符合的资料2. option: 比较的条件符号 eq(等于),ne(不等于),gt(大于),le(小于),cp(包含),np(不包含)3. low: 最小值4. high: 最大值 语法: select-options for example: tables spfli. select-options airline for spfli-connid.将条件的输入值存放入 airline, 筛选选择为spfli中的connid字段执行结果:可直接输入起始范围或按下选择画面, 输入完后按下左上角的执行键 三. 条件输入选择画面1. 自table中选取按下输入项的右边往下箭头, 叫出table中数据项, 选取开始和结束的范围2. selection options按下”selection options”按键, , 输入option及 sign参数内容, 屏幕如下: 3.multi-options输入 按下最右边的multi-options输入键, 输入条件选取的范围, 画面如下: 条件输入完后按下”copy”按键四. 改变条件输入格式1. default to 设定开始结束范围输入默认值 example: select-options airline for spfli-connid default 2042 to 4555.2. no-extension设定不要multi-option输入画面3. no intervals设定不要区间范围输入画面4. lower case输入转换成大写5. obligatory强制要求输入 五. 配合 select 命令 条件输入完后要将符合条件的数据筛选出来, 可配合使用 select 指令 1.使用where example: select-options airline for spfli-connid. select * from spfli where connid in airline. write: / connid,fromcity,tocity. endselect.2.使用check参数example: select-options airline for spfli-connid. select * from spfli. check airline. write: / connid,fromcity,tocity. endselect. 3.使用 if in 叙述 example: select-options airline for spfli-connid. select * from spfli. if spfli-connid in airline. write: / connid,fromcity,tocity. endif endselect. 六. selection-screen 1.产生空白语法: selection-screen skip example: selection-screen skip 2. 产生两列空白列 2.产生底线语法: selection-screen uline / (length) example: selection-screen uline /10(30). 自第10格开始产生长度30的底线 3.印出备注说明 语法: selection-screen comment / (length) example: remark = pls enter your name. selection-screen comment /10(30) remark. 4. 同一列中输入数个数据项语法: selection-screen begin of line. selection-screen end of line. example: selection-screen begin of line. selection-screen position 20. parameters name(10). selection-screen position 40. parameters birth type d. selection-screen end of line. 在20格输入name内容, 40格输入 birth的内容 5. 绘出block panel语法:selection-screen begin of block with frame title . .selection-screen end of block .example: selection-screen begin of block radio with frame . parameter r1 radiobutton group gr1. parameter r2 radiobutton group gr1. parameter r3 radiobutton group gr1. selection-screen end of block radio. sql语法 我们在编写abap4程序的时候,经常需要从table中根据某些条件读取数据,.读取数据最常用的方法就是通过sql语法实现的.abap/4中可以利用sql语法创建或读取table,sql语法分为ddl(data define language)语言和dml(data multipulation language)语言,ddl语言是指数据定义语言,例如create等, dml语言是数据操作语言,例如select, insert等语句. sql语句有open sql语句和native sql语句. open sql语句不是标准sql语句,是abap/4语言,利用open sql语句能在databases 和 command 之间产生一个buffer,所以它有一个语言转换的过程.而native sql语句则是标准的sql语句, 它直接针对databases操作.一. open sql open sql 语句包含有: select,insert,update,modify,delete,open cursor, fetch,close cursor,commit work,rollback work等. 1. select语句语法格式:select into from where group by order by 其中: 指定要抓取的字段 将读取的记录存放在work area中 指定从那个table中读取数据 抓取资料的条件 指定按那些字段分组 排序的字段及方式相关的系统变量: sy-subrc = 0 表示读取数据成功 0 表示未找到符合条件的记录 sy-dblnt: 被处理过的记录的笔数.相关的命令: exit. 退出循环. check .如果逻辑表达式成立,则继续执行,否则,开始下一次循环. .利用循环方式读取所有记录select .endselect.是循环方式读取记录的. 例如:tables mard.select distinct * from mard where matnr = 3520421700. .endselect.(从mard中抓取所有料号=3520421700的数据)读取一笔数据tables mard.select single * from mard where matnr = 3520421700.(从mara中抓取一笔料号=3520421700的资料) 将读取的记录放在work area中,并且加入internal table 中.格式有: . into . into corresponding fields of . into (f1, ., fn) 变量组. . into table . into corresponding fields of table . appending table . appending corresponding fields of table 举例一:tables mard. data: begin of itab occurs 10, matnr like mard-matnr, werks like mard-werks, lgort like mard-lgort, labst like mard-labst, end of itab.select matnr werks lgort labst into corresponding fields of itab from mard where matnr = 3520421700. append itab. clear itab.endselect.(将读取的结果放在internal table itab中)举例二.tables mard.select matnr mtart maktx into (t_matnr, t_mtart, maktx) from mard where matnr = 3520421700. .endselect.(从mard中抓取料号=3520421700的料号、类型和描述,放在变量t_matnr, t_mtart, maktx中)。按指定的字段排序tables sbook.select * from sbook where carrid = lh and connid = 0400 and fldate = 19950228 order by bookid ascending/descending. write: / sbook-bookid, sbook-customid, sbook-custtype, sbook-smoker, sbook-luggweight, sbook-wunit, sbook-invoice.endselect.(利用参数order by所指定的字段排序) 抓取数据的条件叙述(1) between and 例如: where year between 1995 and 2000.(2) like 例如: where name like mike%.(%是通配符号)(3) in ()是里面的任意一个值即可.例如: where plant in (chungli, taoyuan,liutu).(表示plant 只要是chungli或taoyuan或liutu都可以).2. insert 语句 从work area 加入到internal table中格式: insert into values 例如: data: begin of wa, code(6) type c, name(30) type c, end of wa.data: ven like wa occurs 10.wa-code = 530120.wa-name = xingda electronics co.,ltd.insert into ven values wa .如果work area的名称就是internal table的名称,可以直接写成: insert 例如:data: begin of wa occurs 10, code(6) type c, name(30) type c, end of wa.wa-code = 530120.wa-name = xingda electronics co., ltd.insert wa. 从另外一个internal table中insert 数据格式:insert from table accepting duplicate key将中非null的数据加入中,加上accepting duplicate key能限制相同primary key不重复加入.3. modify 语法modify from .4. delete 语法 delete from . 或: delete where 5. database cursor database cursor是一个数据库暂存区, 将经select指令读取的记录存放至此暂存区, 再由此暂存区放至work area中, 可减少数据库读取的次数.1.开启 database cursor 语法: open cursor for select where example: tables spfli. data: wa like spfli, c1 type cursor. open cursor c1 for select * from spfli where area =taiwan. 2.读取 database cursor的数据存入 work area语法: fetch next cursor into example: fetch next cursor c1 into wa. 读取下一笔cursor位置的数据存入wa, 如果已无数据可读, sy-subrc 0. 关闭 database cursor语法: close cursor example: close cursor c1.6. commit work & rollback work要确定数据成功写入数据库,可使用commit work指令,如: commit work.相反的, 如果反悔要复原, 可使用 rollback work, 可复原在上个commit work指令之后的数据, 如: rollback work.使用native sql指令语法格式:exec sql performing . endexec.举例一.exec sql.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论