从内表(internal table)中删除行数据的两种方法
1, 语法
总的说来从内表中删除行数据有两种方法:
1,通过index删除,
1
|
DELETE <internal table> [INDEX <index>].
|
2,通过where语句删除所有符合条件的数据
1
|
DELETE <internal table> [FROM <n1>] [TO <n2>] [WHERE <condition>].
|
2, 代码
测试代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
REPORT ztest_delete_rows.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
id(5) TYPE n,
name(10) TYPE c,
place(10) TYPE c,
age TYPE i,
END OF ty_student.
*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: git_student TYPE TABLE OF ty_student.
gwa_student-id = 1.
gwa_student-name = 'JOHN'.
gwa_student-place = 'London'.
gwa_student-age = 20.
INSERT gwa_student INTO TABLE git_student.
gwa_student-id = 2.
gwa_student-name = 'JIM'.
gwa_student-place = 'New York'.
gwa_student-age = 21.
INSERT gwa_student INTO TABLE git_student.
gwa_student-id = 3.
gwa_student-name = 'JACK'.
gwa_student-place = 'Bangalore'.
gwa_student-age = 20.
INSERT gwa_student INTO TABLE git_student.
gwa_student-id = 4.
gwa_student-name = 'ROB'.
gwa_student-place = 'Bangalore'.
gwa_student-age = 22.
INSERT gwa_student INTO TABLE git_student.
WRITE:/ 'Values in git_student before DELETE' COLOR 4.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
37 'Age' COLOR 5.
LOOP AT git_student INTO gwa_student.
WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
gwa_student-age.
ENDLOOP.
SKIP.
WRITE:/ 'Values in git_student after DELETE' COLOR 4.
*Delete second line from git_student
DELETE git_student INDEX 2.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
37 'Age' COLOR 5.
LOOP AT git_student INTO gwa_student.
WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
gwa_student-age.
ENDLOOP.
SKIP.
WRITE:/ 'Values in git_student after DELETE using WHERE Clause' COLOR 4.
*Delete entries from git_student where place is Bangalore
DELETE git_student WHERE place = 'Bangalore'.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
37 'Age' COLOR 5.
LOOP AT git_student INTO gwa_student.
WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
gwa_student-age.
ENDLOOP.
|
3, 测试
运行结果:
以上。