sap-abap-学习笔记总结_第1页
sap-abap-学习笔记总结_第2页
sap-abap-学习笔记总结_第3页
sap-abap-学习笔记总结_第4页
sap-abap-学习笔记总结_第5页
已阅读5页,还剩74页未读 继续免费阅读

下载本文档

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

文档简介

目录TOC\o"1-5"\h\z\u基本语法 4定义数据和类型 4运算符控制流语句 5字符串操作 7字符串连接 7字符串拆分 7字符串查找 7字符串替换 8字符串长度 8定位操作字符串 9子程序 9无参数 9带参数 9参数传值和传地址对比 10创建功能模块function 10包含程序 11静态变量 11内表操作 111. 定义基本内表 112复杂内表. 12示例一 12示例二 15示例三 16示例四 17数据库建表 21数据库查询 21(一) 单表查询 21(二)多表联合查询 251.嵌套查询 252.FOR

ALL

ENTRIES 263多表结合查询 27结果放入变量 27查询单行,结果放入结构体对象(工作区)中 27查询多行,结果放入内表中 27数据库操作查询学生班级表。 29单表查询 29单行

部分字段 30多表联合查询 30多行查询

部分字段 31单行

所有字段 31单行部分字段 32添加数据 33添加一条 33插入多行数据 33修改记录 34使用变量修改 34使用工作区修改 34使用内表修改多条数据 34修改或保存 35使用工作区修改或保存 35使用内表

修改或保存 35删除 36使用工作区删除 36使用内表删除 36案例:查询数据库数据至内表修改内容并且更新到数据库 36报表设计 37选择屏幕 37选择屏幕参数 37格式化屏幕参数 38复杂选择标准 38ALV报表 40示例一 40参数说明 43w_fcat

TYPE

slis_fieldcat_alv, 43用到属性: 44其他属性: 44w_layo

TYPE

slis_layout_alv, 46使用属性: 46其他属性 46i_fcat

TYPE

slis_t_fieldcat_alv. 49示例二: 52屏幕编程 55重要概念: 55创建: 55举例:用户输入学号返回姓名等信息 57由数据字典生成屏幕 58根据定义的结构生成屏幕 58单选、复选钮用法 58Pov(F4帮助)添加 59屏幕间跳转 59Tablecontrolwithwizard 60添加tablecontrol字段 63设置某列为不可编辑 64设置下拉列表框 64从数据库取值加入列表 65下拉列表取值 66Screen上后添加数据库字段 67Tablecontrol 67Screenlayout 67Screen100中代码 68Programe中代码 69Sartform 70简单示例 70程序部分 70Smartform配置 70Template 70Table做法: 74样式设置 76基本语法定义数据和类型REPORT

ZWANGZHENG2.

*定义数据txt1类型C长度15初始值HELLO

WORLDDATA

txt1(15)

TYPE

c

VALUE

'HELLO

WORLD'.

*定义类型t_txtTYPES

t_txt(15)

TYPE

c

.*定义数据txt2

类型为:t_txt

DATA

txt2

TYPE

t_txt

VALUE

'first'.

*定义类型为N的数据及类型。

DATA

i1(2)

TYPE

n

VALUE

118.

TYPES

my_number_type(5)

TYPE

n.

DATA

i2

TYPE

my_number_type.

*定义时间类型数据

Data

time1

TYPE

t

VALUE

'100701'.

WRITE

time1.

*定义日期类型数据

DATA

day1

TYPE

d.

day1

='20110628'.

DATA

i1

TYPE

I.

i1

=

15.

WRITE

i1.

*定义结构体

TYPES:

BEGIN

OF

student,

studentid

type

I,

sname(20)

type

c,

sex(10)

type

c,

age

type

I,

END

OF

student.

DATA

stu1

TYPE

student.

stu1-studentid

=

1001

.

stu1-sname

=

'tom'.

stu1-sex

=

'man'.

stu1-age

=

18.

WRITE

stu1-studentid

.

WRITE

stu1-sname

.

WRITE

stu1-sex

.

WRITE

stu1-age

.运算符控制流语句REPORT

ZWANGZHENG6.

DATA

a

TYPE

i

VALUE

1.

DATA

b

TYPE

i

VALUE

20.

DATA

c

TYPE

i

.

*a的b次方.c

=

a

**

b

.*b=a+b.

ADD

a

TO

b.

if

a

>

10.

WRITE

/

'>10'

.

elseif

a

>

5

.

WRITE

/

'>5'

.

else

.

WRITE

/

'<5'

.

ENDIF

.

*case语句

CASE

a.

when

12

OR

1

OR

2

.

WRITE

/

'winner'

.

when

3

OR

4

OR

5

.

WRITE

/

'spring'

.

when

6

OR

7

OR

8

.

WRITE

/

'summer'

.

when

9

OR

10

OR

11

.

WRITE

/

'winner'

.

when

others.

WRITE

/

'NO'.

ENDCASE

.

*固定次数循环*输出1-30之间偶数*sy-index循环次数从1开始

DATA

a

TYPE

i

.

DO

30

TIMES.

a

=

sy-index.

DATA

s

TYPE

i

.

s

=

a

MOD

2

.

IF

s

=

0

.

WRITE

/

sy-index

.

ENDIF

.

ENDDO.

DATA

sum1

TYPE

i

value

0

.

DATA

sum2

TYPE

i

value

0

.*循环计算1~100之间的奇数和偶数和。

WHILE

sy-index

<=

100

.

a

=

sy-index

.

b

=

a

mod

2

.

if

b

=

0

.

sum1

=

sum1

+

a

.

else

.

sum2

=

sum2

+

a

.

ENDIF

.

ENDWHILE.

WRITE

/

sum1

.

WRITE

/

sum2

.循环控制CONTINUE:结束本次循环EXIT跳出整个循环相当于BREAK字符串操作字符串连接REPORT

ZWANGZHENG7.

DATA:

s1(20)

TYPE

c

value

'aaa',

s2(20)

TYPE

c

value

'bbb'

,

s3(20)

TYPE

c

,

sep(1)

TYPE

c

value

'.'

.

*s3=s1+s2.中间无空格

CONCATENATE

s1

s2

INTO

s3

.

WRITE

/

s3

.

*s3=s1+s2连接处使用数据sep中的值填充

CONCATENATE

s1

s2

INTO

s3

SEPARATED

BY

sep

.

WRITE

/

s3

.字符串拆分REPORT

ZWANGZHENG8.

DATA:

t

Type

string,

i

TYPE

TABLE

OF

string,

sep

TYPE

c

value

'*'.

t

=

'ABAP*is*a*programming*language'

.

*拆分字符串,把

t

按照

sep

分隔符

拆分

拆分结果放入到

i

表中。

SPLIT

t

At

sep

INTO

TABLE

i

.

*迭代

内表

i

每个元素,将元素

一个个付给

t

LOOP

AT

i

INTO

t.

WRITE

/

t

.

END字符串查找REPORT

ZWANGZHENG9.

DATA

s(30)

TYPE

c

VALUE

'This

is

a

testing

sentence.'

.

WRITE:

/

'Searched'

,

'SY-SUBRC'

,

'SY-FDPOS'.

SEARCH

s

for

'x'

.

WRITE

:/

'x'

,

sy-subrc

UNDER

'SY-SUBRC'

,

SY-fdpos

UNDER

'SY-FDPOS'

.

SEARCH

s

for

'ing

'

.

WRITE

:/

'ing

'

,

sy-subrc

UNDER

'SY-SUBRC'

,

sy-fdpos

UNDER

'SY-FDPOS'

.

SEARCH

s

for

'.ing

'

.

WRITE

:/

'.ing

'

,

sy-subrc

UNDER

'SY-SUBRC'

,

sy-fdpos

UNDER

'SY-FDPOS'

.

SEARCH

s

for

's*'

.

WRITE

:/

's*'

,

sy-subrc

UNDER

'SY-SUBRC'

,

sy-fdpos

UNDER

'SY-FDPOS'

.

SEARCH

s

for

'*s'

.

WRITE

:/

'*s'

,

sy-subrc

UNDER

'SY-SUBRC'

,

sy-fdpos

UNDER

'SY-FDPOS'

.字符串替换REPORT

zwangzheng10.

DATA

s

TYPE

string.

s

=

'aaa-ss-dd-fff-ggg-sss'.

WRITE

sy-subrc

."sy-subrc初始值为0

WHILE

sy-subrc

=

0

.

REPLACE

'-'

WITH

'

'

INTO

s

.

ENDWHILE

.

WRITE

s

.字符串长度REPORT

zwangzheng11.

DATA:

s

TYPE

string

VALUE

'123456789'

,

len

TYPE

i

.

len

=

STRLEN(

s

).

WRITE

len

.定位操作字符串REPORT

ZWANGZHENG12.

DATA:

s1(8)

TYPE

c

VALUE

'abcdefgh'

,

s2(20)

TYPE

c

value

'1234567890'

.*将s2从下标为2的位子开始往后3个字符串,*替换成s1从下标为2的位子开始往后3个字符串

s2+2(3)

=

s1+2(3)

.

WRITE

s1

.

WRITE

s2

.子程序无参数data:

a

type

i

value

1

,

b

type

i

value

2,

sum

type

i.

perform

add.

write

sum.

form

add.

sum

=

a

+

b.

endform.带参数

DATA:

a

TYPE

i

,

b

TYPE

i

,

c

TYPE

i

,

d

TYPE

i

,

sum

TYPE

i.

START-OF-SELECTION.

a

=

1.

b

=

2.

PERFORM

add

USING

a

b.

WRITE

sum.

FORM

add

USING

c

d.

sum

=

c

+

d.

WRITE

sum.

ENDFORM.

参数传值和传地址对比

DATA:

a

TYPE

i

,

b

TYPE

i

.

START-OF-SELECTION.

a

=

1.

b

=

1.

"a传地址

b传值

PERFORM

add

USING

a

b.

write

:

/

a

,

b.

*输出结果21

FORM

add

USING

c

type

i

value(d)

type

i.

c

=

c

+

1.

d

=

d

+

1.

ENDFORM.创建功能模块function1)t-code:SE37进入2)gotofuctiongroupcreategroup输入组名。操作按书p154-p158操作。包含程序被包含程序不需要programreport进行引导,不能独立运行,不能与包含程序之间传数据。创建方法:创建programtype:includeprogram.*&

*&

Include

ZTEST7_15_HEAD

*&*

WRITE:

/

'系统用户:'

,sy-uname

,

',所在服务器:',

sy-host,

',日期:',sy-datum

,',时间',sy-uzeit.

ULINE.调用包含程序程序中调用:include

ZTEST7_15_HEAD.静态变量声明:STATICS

a

TYPE

i.内表操作定义基本内表实例1:*定义结构体

line

DATA:

BEGIN

OF

line

,

num

TYPE

i,

sqr

TYPE

i,

END

OF

line

.

*定义内表itab

按照数据line存储。并且制定整行为关键字

DATA

itab

LIKE

STANDARD

TABLE

OF

line

WITH

KEY

table_line.

DO

5

TIMES

.

line-num

=

sy-index

.

line-sqr

=

sy-index

**

2

.

APPEND

line

TO

itab.

ENDDO

.

LOOP

AT

itab

INTO

line

.

WRITE:

/

line-num

,line-sqr.

ENDLOOP.

CLEAR

itab."清空内表实例2:REPORT

zwangzheng14.

TYPES:BEGIN

OF

address,

street(20)

TYPE

c,

city(20)

TYPE

c,

END

OF

address

.

DATA:

BEGIN

OF

company,

name(20)

TYPE

c,

address

TYPE

address,

END

OF

company.

DATA

itab_company

LIKE

HASHED

TABLE

OF

company

WITH

UNIQUE

KEY

name.

DATA

itab_company_sorted

LIKE

SORTED

TABLE

OF

company

WITH

UNIQUE

KEY

name.2复杂内表.示例一*定义结构

address

DATA:

BEGIN

OF

address,

street(20)

type

c,

city(20)

type

c,

END

OF

address.

*定义标准内表address_tab

,结构为:address

TYPES

address_tab

LIKE

STANDARD

TABLE

OF

address

WITH

NON-UNIQUE

KEY

city.

*定义结构

company

包含

address_tab属性

DATA:

BEGIN

OF

company,

name(20)

TYPE

c,

addresses

TYPE

address_tab,

END

OF

company.

*定义哈希内表company_tab,结构:company

DATA

company_tab

LIKE

HASHED

TABLE

OF

company

WITH

UNIQUE

KEY

name.

*定义变量idx

:内表下标

DATA

idx

TYPE

sy-tabix.

*创建一个

company

结构体数据:麦当劳。

company-name

=

'麦当劳'.

address-street

=

'太原街'.

address-city

=

'沈阳'.

APPEND

address

to

company-addresses.

address-street

=

'中街'.

address-city

=

'沈阳'.

APPEND

address

to

company-addresses.

INSERT

company

INTO

TABLE

company_tab.

CLEAR

company.

*创建一个

company

结构体数据:肯德基。

company-name

=

'肯德基'.

address-street

=

'王府井'.

address-city

=

'北京'.

APPEND

address

to

company-addresses.

address-street

=

'南京路'.

address-city

=

'上海'.

APPEND

address

to

company-addresses.

INSERT

company

INTO

TABLE

company_tab.

CLEAR

company.

*

*通过

KEY

name

读取表

装入company结构

READ

TABLE

company_tab

WITH

KEY

name

=

'肯德基'

INTO

company.

WRITE

/

company-name.

LOOP

AT

company-addresses

INTO

address.

WRITE:

/

sy-tabix

,

address-street

,

address-city

.

ENDLOOP.

*

*

修改内表

address-street

=

'五爱街'.

address-city

=

'沈阳'.

READ

TABLE

company_tab

WITH

TABLE

KEY

name

=

'麦当劳'

INTO

company.

*读取

company-addresses

对应内表--address_tab

READ

TABLE

company-addresses

TRANSPORTING

NO

FIELDS

WITH

TABLE

KEY

city

=

address-city

.

idx

=

sy-tabix.

"操作内表

操作完

返回行记录索引,未操作

返回0.

*修改company-addresses对应内表

将索引为idx的记录

用address对象替代

MODIFY

company-addresses

FROM

address

INDEX

idx

.

*?????????????未指定条件

MODIFY

TABLE

company_tab

from

company

.

*

*读取表信息

*key

字段可以使任何字段,不一定是关键字段

READ

TABLE

company_tab

WITH

KEY

name

=

'麦当劳'

INTO

company.

WRITE

/

company-name.

LOOP

AT

company-addresses

INTO

address.

WRITE:

/

sy-tabix

,

address-street

,

address-city

.

ENDLOOP.

*示例二REPORT

ZWANGZHENG17.

DATA:

BEGIN

OF

student,

sid

TYPE

n,

name(20)

TYPE

c,

END

OF

student.

TYPES:

stu_tab

LIKE

STANDARD

TABLE

OF

student

WITH

KEY

sid.

DATA:

BEGIN

OF

class

,

cid

TYPE

n,

name(20)

TYPE

c,

students

TYPE

stu_tab,

END

OF

class.

DATA:

class_tab

LIKE

STANDARD

TABLE

OF

class

WITH

KEY

cid.

class-cid

=

1.

class-name

=

'软件一班'.

student-sid

=

1.

student-name

=

'Tom'.

APPEND

student

to

class-students.

student-sid

=

2.

student-name

=

'Lucy'.

APPEND

student

to

class-students.

student-sid

=

3.

student-name

=

'Lily'.

APPEND

student

to

class-students.

INSERT

class

INTO

TABLE

class_tab.

CLEAR

class.

class-cid

=

2.

class-name

=

'软件二班'.

student-sid

=

1.

student-name

=

'李雷'.

APPEND

student

to

class-students.

student-sid

=

2.

student-name

=

'韩梅梅'.

APPEND

student

to

class-students.

INSERT

class

INTO

TABLE

class_tab.

CLEAR

class.

*读取数据

READ

TABLE

class_tab

WITH

TABLE

KEY

cid

=

1

INTO

class.

WRITE:

/

class-cid,

class-name.

LOOP

AT

class-students

INTO

student.

WRITE:

/

student-sid

,

student-name

.

ENDLOOP.示例三REPORT

ZWANGZHENG18.

DATA:

BEGIN

OF

student,

sid

TYPE

n,

name(20)

TYPE

c,

END

OF

student.

TYPES:

stu_tab

LIKE

STANDARD

TABLE

OF

student

WITH

KEY

sid.

DATA:

BEGIN

OF

class

,

cid

TYPE

n,

name(20)

TYPE

c,

students

TYPE

stu_tab,

END

OF

class.

DATA:

class_tab

LIKE

STANDARD

TABLE

OF

class

WITH

KEY

cid.

class-cid

=

1.

class-name

=

'软件一班'.

student-sid

=

1.

student-name

=

'Tom'.

APPEND

student

to

class-students.

student-sid

=

2.

student-name

=

'Lucy'.

APPEND

student

to

class-students.

student-sid

=

3.

student-name

=

'Lily'.

APPEND

student

to

class-students.

INSERT

class

INTO

TABLE

class_tab.

CLEAR

class.

class-cid

=

2.

class-name

=

'软件二班'.

student-sid

=

1.

student-name

=

'李雷'.

APPEND

student

to

class-students.

student-sid

=

2.

student-name

=

'韩梅梅'.

APPEND

student

to

class-students.

INSERT

class

INTO

TABLE

class_tab.

CLEAR

class.

*读取数据

"READ

TABLE

class_tab

WITH

TABLE

KEY

cid

=

1

INTO

class.

"WRITE:

/

class-cid,

class-name.

LOOP

AT

class_tab

INTO

class.

WRITE:

/

class-cid,

class-name.

LOOP

AT

class-students

INTO

student.

WRITE:

/

student-sid

,

student-name

.

ENDLOOP.

WRITE

/.

ENDLOOP.示例四

REPORT

ZWANGZHENG19.

DATA:

BEGIN

OF

student,

sid

TYPE

n,

name(20)

TYPE

c,

END

OF

student.

TYPES:

stu_tab

LIKE

STANDARD

TABLE

OF

student

WITH

KEY

sid.

DATA:

BEGIN

OF

class

,

cid

TYPE

n,

name(20)

TYPE

c,

students

TYPE

stu_tab,

END

OF

class.

TYPES:

class_tab

LIKE

STANDARD

TABLE

OF

class

WITH

KEY

cid.

DATA:

BEGIN

OF

dept

,

did

TYPE

n,

name(20)

TYPE

c,

classes

TYPE

class_tab,

END

OF

dept.

DATA:

dept_tab

LIKE

STANDARD

TABLE

OF

dept

WITH

KEY

did.

dept-did

=

1.

dept-name

=

'信息学院'.

class-cid

=

1.

class-name

=

'软件一班'.

student-sid

=

1.

student-name

=

'Tom'.

APPEND

student

to

class-students.

student-sid

=

2.

student-name

=

'Lucy'.

APPEND

student

to

class-students.

student-sid

=

3.

student-name

=

'Lily'.

APPEND

student

to

class-students.

APPEND

class

TO

dept-classes.

CLEAR

class.

class-cid

=

2.

class-name

=

'软件二班'.

student-sid

=

1.

student-name

=

'李雷'.

APPEND

student

to

class-students.

student-sid

=

2.

student-name

=

'韩梅梅'.

APPEND

student

to

class-students.

"INSERT

class

INTO

TABLE

class_tab.

APPEND

class

TO

dept-classes.

CLEAR

class.

INSERT

dept

INTO

TABLE

dept_tab.

CLEAR

DEPT.

dept-did

=

2.

dept-name

=

'法学院'.

class-cid

=

1.

class-name

=

'民法一班'.

student-sid

=

1.

student-name

=

'张三'.

APPEND

student

to

class-students.

student-sid

=

2.

student-name

=

'李四'.

APPEND

student

to

class-students.

student-sid

=

3.

student-name

=

'王五'.

APPEND

student

to

class-students.

APPEND

class

TO

dept-classes.

CLEAR

class.

class-cid

=

2.

class-name

=

'宪法一班'.

student-sid

=

1.

student-name

=

'刘德华'.

APPEND

student

to

class-students.

student-sid

=

2.

student-name

=

'张惠妹'.

APPEND

student

to

class-students.

"INSERT

class

INTO

TABLE

class_tab.

APPEND

class

TO

dept-classes.

CLEAR

class.

INSERT

dept

INTO

TABLE

dept_tab.

CLEAR

DEPT.

*读取数据

LOOP

AT

dept_tab

INTO

dept.

WRITE:

/

dept-did,

dept-name

.

WRITE:

/.

LOOP

AT

dept-classes

INTO

class.

WRITE:

/

'

',

class-cid,

class-name.

LOOP

AT

class-students

INTO

student.

WRITE:

/

'

',

student-sid

,

student-name

.

ENDLOOP.

WRITE

/.

ENDLOOP.

WRITE

/.

ENDLOOP.数据库建表先建表输入dataelement名(不存在),双击名字,去创建,创建过程中输入DOMAIN名(不存在),双击创建,选择数据类型和长度nocharactor数据库查询示例表spfliscar单表查询

1)单行查询

1.1所有字段,将结果装入结构体中。

DATA

wa_spfli

TYPE

spfli.

SELECT

SINGLE

*

FROM

spfli

INTO

wa_spfli

WHERE

cityfrom

=

'SINGAPORE'

.

WRITE:

/

wa_spfli-mandt

,

wa_spfli-carrid

,

wa_spfli-connid

,

wa_spfli-countryfr

,

wa_spfli-countryfr

,

wa_spfli-cityfrom

,

wa_spfli-cityto

.

"

运行结果:

800

QF

0005

SG

SG

SINGAPORE

FRANKFURT

"

结果为第一个符合条件的数据行。

1.2部分字段

1.2.1结果装入多个基本数据类型

DATA:

wa_mandt

TYPE

spfli-mandt,

wa_carrid

TYPE

spfli-carrid,

wa_connid

TYPE

spfli-connid.

SELECT

SINGLE

mandt

carrid

connid

FROM

spfli

INTO

(wa_mandt,wa_carrid,wa_connid)

WHERE

cityfrom

=

'SINGAPORE'

.

WRITE:

/

wa_mandt,

wa_carrid,

wa_connid

.

"运行结果:800

QF

0005

1.2.2结果装入结构体中

*效率不如装入多个基本数据类型高。

data

wa_spfli

type

spfli.

SELECT

SINGLE

mandt

carrid

connid

from

spfli

into

corresponding

fields

of

wa_spfli

where

cityfrom

=

'SINGAPORE'

.

WRITE:

/

wa_spfli-mandt,

wa_spfli-carrid,

wa_spfli-connid

.

"运行结果相同,效率较低

2)多行查询

2.1所有字段

2.1.1循环查询

装入结构

并且输出。

data

wa_spfli

type

spfli.

SELECT

*

FROM

spfli

INTO

wa_spfli

WHERE

cityfrom

=

'SINGAPORE'

.

write:

/

wa_spfli-mandt,

wa_spfli-carrid,

wa_spfli-connid

,

wa_spfli-countryfr

,

wa_spfli-cityfrom

,

wa_spfli-cityto.

endselect.

*输出结果:

*800

QF

0005

SG

SINGAPORE

FRANKFUR

*800

SQ

0002

SG

SINGAPORE

SAN

FRAN

*800

SQ

0158

SG

SINGAPORE

JAKARTA

*800

SQ

0988

SG

SINGAPORE

TOKYO

"将都装入一个结构体中,每次循环会被替换。

2.1.2

查询

装入内表。

DATA

spfli_tab

TYPE

TABLE

OF

spfli.

"按照spfli结构定义内表

SELECT

*

FROM

spfli

INTO

TABLE

spfli_tab.

"迭代内表。

DATA

wa_spfli

TYPE

spfli.

"负责存贮每次迭代出的数据

LOOP

AT

spfli_tab

INTO

wa_spfli.

write:

/

wa_spfli-mandt,

wa_spfli-carrid,

wa_spfli-connid

,

wa_spfli-countryfr

,

wa_spfli-cityfrom

,

wa_spfli-cityto.

ENDLOOP.

*运行结果:

*800

AA

0017

US

NEW

YORK

SAN

FRANCISCO

*800

AA

0064

US

SAN

FRANCISCO

NEW

YORK

*800

AZ

0555

IT

ROME

FRANKFURT

*800

AZ

0788

IT

ROME

TOKYO

*800

AZ

0789

JP

TOKYO

ROME

*800

AZ

0790

IT

ROME

OSAKA

*800

DL

0106

US

NEW

YORK

FRANKFURT

*800

DL

1699

US

NEW

YORK

SAN

FRANCISCO

*800

DL

1984

US

SAN

FRANCISCO

NEW

YORK

*800

JL

0407

JP

TOKYO

FRANKFURT

*800

JL

0408

DE

FRANKFURT

TOKYO

*800

LH

0400

DE

FRANKFURT

NEW

YORK

*800

LH

0401

US

NEW

YORK

FRANKFURT

*800

LH

0402

DE

FRANKFURT

NEW

YORK

*800

LH

2402

DE

FRANKFURT

BERLIN

*800

LH

2407

DE

BERLIN

FRANKFURT

*800

QF

0005

SG

SINGAPORE

FRANKFURT

*800

QF

0006

DE

FRANKFURT

SINGAPORE

*800

SQ

0002

SG

SINGAPORE

SAN

FRANCISCO

*800

SQ

0015

US

SAN

FRANCISCO

SINGAPORE

*800

SQ

0158

SG

SINGAPORE

JAKARTA

*800

SQ

0988

SG

SINGAPORE

TOKYO

*800

UA

0941

DE

FRANKFURT

SAN

FRANCISCO

*800

UA

3504

US

SAN

FRANCISCO

FRANKFURT

*800

UA

3516

US

NEW

YORK

FRANKFURT

*800

UA

3517

DE

FRANKFURT

NEW

YORK

2.2部分字段

2.2.1

查询

装入内表。

"定义结构

与查询字段列表结构相同

TYPES:

BEGIN

OF

tab_type,

mandt

TYPE

spfli-mandt

,

carrid

TYPE

spfli-carrid

,

connid

TYPE

spfli-connid

,

END

OF

tab_type.

data

spfli_tab2

type

table

of

tab_type.

"定义内表类型

SELECT

mandt

carrid

connid

FROM

spfli

into

corresponding

fields

of

table

spfli_tab2

where

carrid

=

'AA'.

"迭代内表

data

t_t

type

tab_type.

loop

at

spfli_tab2

into

t_t.

write:

/

t_t-mandt,

t_t-carrid

,

t_t-connid.

endloop.

*运行结果:

*800

AA

0017

*800

AA

0064(二)多表联合查询1.嵌套查询data:

wa_carrid

type

spfli-carrid,

wa_connid

type

spfli-connid,

wa_carrname

type

scarr-carrname.

select

carrid

connid

from

spfli

into

(wa_carrid

,

wa_connid)

where

cityfrom

=

'SAN

FRANCISCO'

.

write:

/

wa_carrid

,

wa_connid.

select

carrname

from

scarr

into

wa_carrname

where

carrid

=

wa_carrid."查询结果与表数据比较

write

wa_carrname.

endselect.

endselect.2.FOR

ALL

ENTRIESDATA:

BEGIN

OF

wa_spfli,

"!!!!!!

"types:

BEGIN

OF

wa_spfli,

"

carrid

TYPE

spfli-carrid,

connid

TYPE

spfli-connid,

END

OF

wa_spfli.

DATA

spfli_tab

LIKE

TABLE

OF

wa_spfli.

"!!!!!!

"data

spfli_table

type

table

of

wa_spfli.

"

*与,!!!!!!与!!!!!!

组合使用

data:

begin

of

wa_scarr,

carrid

type

scarr-carrid

,

carrname

type

scarr-carrname

,

end

of

wa_scarr.

SELECT

carrid

connid

FROM

spfli

INTO

TABLE

spfli_tab

WHERE

cityfrom

=

'SAN

FRANCISCO'

.

select

carrid

carrname

from

scarr

into

wa_scarr

for

all

entries

in

spfli_tab“对应字段插入值

where

carrid

=

spfli_tab-carrid.

write:

/

wa_scarr-carrid

,

wa_scarr-carrname.

endselect.

"输出结果

*AA

American

Airlines

*DL

Delta

Airlines

*SQ

Singapore

Airlines

*UA

United

Airlines3多表结合查询结果放入变量DATA:

wa_carrid

TYPE

spfli-carrid,

wa_connid

TYPE

spfli-connid,

wa_carrname

TYPE

scarr-carrname.

SELECT

spfli~carrid

scarr~carrname

spfli~connid

FROM

spfli

INNER

JOIN

scarr

ON

scarr~carrid

=

spfli~carrid

INTO

(wa_carrid

,

wa_carrname,wa_connid)

WHERE

spfli~cityfrom

=

'SINGAPORE'.

write:

/

wa_carrid

,

wa_connid,

wa_carrname.

ENDSELECT.查询单行,结果放入结构体对象(工作区)中

TYPES:BEGIN

OF

wa_spfli_type,

carrid

TYPE

spfli-carrid,

connid

TYPE

spfli-connid,

carrname

TYPE

scarr-carrname,

END

OF

wa_spfli_type.

DATA

wa_spfli

TYPE

wa_spfli_type.

SELECT

SINGLE

spfli~carrid

spfli~connid

scarr~carrname

FROM

spfli

INNER

JOIN

scarr

ON

scarr~carrid

=

spfli~carrid

INTO

wa_spfli

WHERE

spfli~cityfrom

=

'SINGAPORE'.

write:

/

wa_spfli-carrid

,

wa_spfli-connid

,

wa_spfli-carrname

.查询多行,结果放入内表中

TYPES:BEGIN

OF

wa_spfli_type,

carrid

TYPE

spfli-carrid,

connid

TYPE

spfli-connid,

carrname

TYPE

scarr-carrname,

END

OF

wa_spfli_type.

data

spfil_tab

type

table

of

wa_spfli_type.

select

spfli~carrid

spfli~connid

scarr~carrname

from

spfli

inner

join

scarr

on

scarr~carrid

=

spfli~carrid

into

corresponding

fields

of

table

spfil_tab

.

data

wa_spfli

type

wa_spfli_type.

loop

at

spfil_tab

into

wa_spfli.

write:

/

wa_spfli-carrid

,wa_spfli-connid

,

wa_spfli-carrname.

endloop.

*输出结果

*AA

0017

American

Airlines

*AA

0064

American

Airlines

*AZ

0555

Alitalia

*AZ

0788

Alitalia

*AZ

0789

Alitalia

*AZ

0790

Alitalia

*DL

0106

Delta

Airlines

*DL

1699

Delta

Airlines

*DL

1984

Delta

Airlines

*JL

0407

Japan

Airlines

*JL

0408

Japan

Airlines

*LH

0400

Lufthansa

*LH

0401

Lufthansa

*LH

0402

Lufthansa

*LH

2402

Lufthansa

*LH

2407

Lufthansa

*QF

0005

Qantas

Airways

*QF

0006

Qantas

Airways

*SQ

0002

Singapore

Airline

*SQ

0015

Singapore

Airline

*SQ

0158

Singapore

Airline

*SQ

0988

Singapore

Airline

*UA

0941

United

Airlines

*UA

3504

United

Airlines

*UA

3516

United

Airlines

*UA

3517

United

Airlines数据库操作查询学生班级表。使用表zstudentzclass单表查询

多行

所有字段。

*Data

stu_tab

type

table

of

zstudent.

*data

wa_stu

type

zstudent.

*select

*

*

from

zstudent

*

*

into

table

stu_tab

*

order

by

zstudent~sid.

*

*

loop

at

stu_tab

into

wa_stu.

*

write:

/

wa_stu-sid

,

wa_stu-sname

,

wa_stu-sex

,

wa_stu-age

.

*

endloop.

多行部分字段

*TYPES:

BEGIN

OF

stu_type,

*

sid

TYPE

zstudent-sid,

*

sname

TYPE

zstudent-sname,

*

END

OF

stu_type.

*

*DATA

stu_tab

TYPE

TABLE

OF

stu_type.

*DATA

wa_stu

TYPE

stu_type.

*

*SELECT

sid

sname

*

FROM

zstudent

*

INTO

TABLE

stu_tab

*

WHERE

cid

=

1.

*

*LOOP

AT

stu_tab

INTO

wa_stu.

*

write:

/

wa_stu-sid

,

wa_stu-sname.

*ENDLOOP.

单行

所有字段

*data

wa_stu

type

zstudent.

*

*select

single

*

*

from

zstudent

*

into

wa_stu

*

where

sid

=

1

.

*

*

write:

wa_stu-sid

,

wa_stu-sname

,

wa_stu-cid

.

单行

部分字段

*TYPES:

BEGIN

OF

stu_type,

*

sid

TYPE

zstudent-sid,

*

sname

TYPE

zstudent-sname,

*

cid

TYPE

zstudent-cid,

*

END

OF

stu_type.

*

*DATA

wa_stu

TYPE

stu_type.

*

*SELECT

SINGLE

sid

sname

cid

*

FROM

zstudent

*

INTO

wa_stu

*

WHERE

sid

=

11.

*

*

write:

/

wa_stu-sid

,

wa_stu-sname

,

wa_stu-cid.多表联合查询

多行查询

所有字段

TYPES:

BEGIN

OF

stu_class_type,

sid

TYPE

zstudent-sid

,

sname

TYPE

zstudent-sname,

sex

TYPE

zstudent-sex,

age

TYPE

zstudent-age,

cid

TYPE

zclass-cid

,

cname

TYPE

zclass-cname

,

address

TYPE

zclass-address

,

END

OF

stu_class_type.

DATA

stu_class_tab

TYPE

TABLE

OF

stu_class_type.

SELECT

*

FROM

zstudent

JOIN

zclass

ON

zstudent~cid

=

zclass~cid

INTO

CORRESPONDING

FIELDS

OF

TABLE

stu_class_tab

WHERE

zstudent~cid

=

1.

DATA

wa_stu_class

TYPE

stu_class_type.

LOOP

AT

stu_class_tab

into

wa_stu_class.

write:

/

wa_stu_class-sid

,

wa_stu_class-sname

,

wa_stu_class-cid

,

wa_stu_class-cname

,wa_stu_class-address.

endloop

.多行查询

部分字段

types:

begin

of

stu_class_type,

sid

type

zstudent-sid,

sname

type

zstudent-sname,

cid

type

zclass-cid,

cname

type

zclass-cname,

end

of

stu_class_type.

data

stu_class_tab

type

table

of

stu_class_type.

data

wa_stu_class

type

stu_class_type.

select

zstudent~sid

zstudent~sname

zclass~cid

zclass~cname

from

zstudent

join

zclass

on

zstudent~cid

=

zclass~cid

into

table

stu_class_tab

where

zclass~cid

=

2

.

loop

at

stu_class_tab

into

wa_stu_class.

write:

/

wa_stu_class-sid,

wa_stu_class-sname

,

wa_stu_class-cid

,

wa_stu_class-cname.

endloop.单行

所有字段

TYPES:

BEGIN

OF

stu_class_type,

sid

TYPE

zstudent-sid

,

sname

TYPE

zstudent-sname,

sex

TYPE

zstudent-sex,

age

TYPE

zstudent-age,

cid

TYPE

zclass-cid

,

cname

TYPE

zclass-cname

,

address

TYPE

zclass-address

,

END

OF

stu_class_type.

data

wa_stu_class

type

stu_class_type.

select

single

*

from

zstudent

join

zclass

on

zstudent~cid

=

zclass~cid

"对应同名字段。

into

corresponding

fields

of

wa_stu_class

where

zstudent~sid

=

12.

write:

/

wa_stu_class-sid

,

wa_stu_class-sname

,

wa_stu_class-sex

,

wa_stu_class-cid

,

wa_stu_class-cname

,

wa_stu_class-address.单行部分字段

types:

begin

of

stu_class_type,

sid

type

zstudent-sid,

sname

type

zstudent-sname,

cid

type

zclass-cid,

cname

type

zclass-cname,

end

of

stu_class_type.

data

wa_stu_class

type

stu_class_type.

select

single

zstudent~sid

zstudent~sname

zclass~cid

zclass~cname

from

zstudent

join

zclass

on

zstudent~cid

=

zclass~cid

into

wa_stu_class

where

zstudent~sid

=

13.

write:

/

wa_stu_class-sid

,

wa_stu_class-sname

,

wa_stu_class-cid

,

wa_stu_class-cname

.添加数据添加一条DATA

wa_stu

TYPE

zstudent.

wa_stu-sid

=

102.

wa_stu-sname

=

'hanmeimei'.

wa_stu-sex

=

'女'.

wa_stu-age

=

19.

wa_stu-cid

=

1.

INSERT

into

zstudent

values

wa_stu.插入多行数据DATA

stu_tab

TYPE

TABLE

OF

zstudent.

DATA

wa_stu

TYPE

zstudent.

wa_stu-sid

=

103.

wa_stu-sname

=

'刘德华'.

wa_stu-sex

=

'男'.

wa_stu-age

=

50.

wa_stu-cid

=

1.

APPEND

wa_stu

TO

stu_tab.

wa_stu-sid

=

104.

wa_stu-sname

=

'

张学友'.

wa_stu-sex

=

'男'.

wa_stu-age

=

49.

wa_stu-cid

=

1.

APPEND

wa_stu

TO

stu_tab.

INSERT

zstudent

FROM

TABLE

stu_tab.修改记录使用变量修改

update

zstudent

set

sname

=

'韩梅梅'

age

=

20

where

sid

=

102.

使用工作区修改

DATA

wa_stu

TYPE

zstudent.

*数据必须填写全,不填项将被设置为空。

wa_stu-sid

=

101.

wa_stu-sname

=

'李磊'.

wa_stu-sex

=

'男'.

wa_stu-age

=

20.

wa_stu-cid

=

1.

update

zstudent

from

wa_stu.使用内表修改多条数据

data

stu_tab

type

table

of

zstudent.

data

wa_stu

type

zstudent.

wa_stu-sid

=

10.

wa_stu-sname

=

'lily'.

wa_stu-sex

=

'女'.

wa_stu-age

=

16.

wa_stu-cid

=

1.

append

wa_stu

to

stu_tab.

wa_stu-sid

=

11.

wa_stu-sname

=

'lucy'.

wa_stu-sex

=

'女'.

wa_stu-age

=

16.

wa_stu-cid

=

1.

append

wa_stu

to

stu_tab.

wa_stu-sid

=

12.

wa_stu-sname

=

'tom'.

wa_stu-sex

=

'男'.

wa_stu-age

=

18.

wa_stu-cid

=

1.

append

wa_stu

to

stu_tab.

wa_stu-sid

=

13.

wa_stu-sname

=

'jim'.

wa_stu-sex

=

'男'.

wa_stu-age

=

19.

wa_stu-cid

=

1.

append

wa_stu

to

stu_tab.

update

zstudent

from

table

stu_tab.修改或保存使用工作区修改或保存

data

wa_stu

type

zstudent.

wa_stu-sid

=

2.

wa_stu-sname

=

'周杰伦'.

wa_stu-sex

=

'男'.

wa_stu-age

=

32.

wa_stu-cid

=

1.

*当主键存在时修改否则添加。

modify

zstudent

from

wa_stu.使用内表

修改或保存

data

stu_tab

type

table

of

zstudent.

data

wa_stu

type

zstudent.

wa_stu-sid

=

13.

wa_stu-sname

=

'Jim

Green'.

wa_stu-sex

=

'男'.

wa_stu-age

=

18.

wa_stu-cid

=

1.

append

wa_stu

to

stu_tab.

wa_stu-sid

=

14.

wa_stu-sname

=

'王叔叔'.

wa_stu-sex

=

'男'.

wa_stu-age

=

60.

wa_stu-cid

=

1.

append

wa_stu

to

stu_tab.

modify

zstudent

from

table

stu_tab.删除

使用条件

必须列出所有关键字

delete

from

zstudent

where

sid

=

1.使用工作区删除

*仅需要为工作区赋值所有关键字

data

wa_stu

type

zstudent.

wa_stu-sid

=

100.

delete

zstudent

from

wa_stu.使用内表删除

data

stu_tab

type

table

of

zstudent.

data

wa_stu

type

zstudent.

wa_stu-sid

=

11.

append

wa_stu

to

stu_tab.

wa_stu-sid

=

12.

append

wa_stu

to

stu_tab.

wa_stu-sid

=

13.

append

wa_stu

to

stu_tab.

wa_stu-sid

=

14.

append

wa_stu

to

stu_tab.

delete

zstudent

from

table

stu_tab.案例:查询数据库数据至内表修改内容并且更新到数据库*练习将zstudent表中数据

查询

装入内表,循环内表

将所有人age+1

sname后

+先生、女士

DATA

stu_tab

TYPE

TABLE

OF

zstudent.

DATA

wa_stu

TYPE

zstudent.

SELECT

*

FROM

zstudent

INTO

CORRESPONDING

FIELDS

OF

TABLE

stu_tab.

LOOP

AT

stu_tab

INTO

wa_stu.

WRITE:

/

wa_stu-sid

,wa_stu-sname

,

wa_stu-sex

,wa_stu-age

,wa_stu-cid.

wa_stu-age

=

wa_stu-age

+

10.

IF

wa_stu-sex

=

'男'

.

concatenate

wa_stu-sname

'先生'

into

wa_stu-sname.

ELSEIF

wa_stu-sex

=

'女'.

concatenate

wa_stu-sname

'女士'

into

wa_stu-sname.

ELSE

.

concatenate

wa_stu-sname

'同志'

into

wa_stu-sname.

ENDIF.

"WRITE:

/

'--'

,wa_stu-sid

,wa_stu-sname

,

wa_stu-sex

,wa_stu-age

,wa_stu-cid.

modify

zstudent

from

wa_stu."更新回数据库

"MODIFY

stu_tab

FROM

wa_stu

."或者先将工作区内容更新回内表

ENDLOOP.

"modify

zstudent

from

table

stu_tab."将内表整体更新回数据库报表设计选择屏幕选择屏幕参数DATA

stu_tab

TYPE

TABLE

OF

zstudent.

DATA

wa_stu

TYPE

zstudent.

PARAMETERS

p_sex(4)

TYPE

c.

SELECT

*

FROM

zstudent

INTO

CORRESPONDING

FIELDS

OF

TABLE

stu_tab

WHERE

sex

=

p_sex.

LOOP

AT

stu_tab

INTO

wa_stu.

write:

/

wa_stu-sid

,

wa_stu-sname

,

wa_stu-sex

,

wa_stu-age

,

wa_stu-cid.

ENDLOOP.格式化屏幕参数

parameters

sname(20)

type

c

default

'姓名'.

"默认值

parameters

age

type

i

obligatory

."必填******单选框使用*******PARAMETERS:

r1

RADIOBUTTON

GROUP

rad1

,

r2

RADIOBUTTON

GROUP

rad1

,

r3

RADIOBUTTON

GROUP

rad1

DEFAULT

'X'

.

IF

r1

=

'X'.

WRITE:

/

'r1'.

ELSEIF

r2

=

'X'.

WRITE:

/

'r2'.

ELSE.

WRITE:

/

'r3'.

ENDIF.复杂选择标准data

wa_zstudent

type

zstudent.

*设置选择条件

select-options

so_stu

for

温馨提示

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

评论

0/150

提交评论