abap的数据库操作
【转 http://blog.pconline.com.cn/article/265316.html】
ABAP数据库操作(
1、abap语言使用
2、使用OPen SQL注意的原则: 
a、尽可能减少满足条
b、减少数据的传输量
c、减少访问的数据库
d、减少查询难度,可
e、减少数据库负载。
3、使用Native
a、知道使用数据库的
b、了解该数据库的S
4、ABAP的数据定
5、提取数据方式:内
6、select语句
select <result> from <source> into <target> 
where <condition
[having <cond>][or
7、选择单行全部数据
select single * from spfli into wa_spfli where cityform=’
cityto=’be
8、选择单行指定字段
select single carrid connid from spfli into (wa_carrid
and into cityto=’be
9、选择相关字段: 
select single carrid connid *from spfli into correspond
wa_spfli where cityform=’
10、循环选择: 
select * 
from spfli into wa_spfli. 
write:/ wa_spfli-c
endselect.
11、选择至内表: 
select * 
from spfli into table ta_spfli. 
读取时: 
loop at ta_spfli. 
write:/ta_
end loop. 
12、指定查询条件 
比较运算符:= < > <> <= >= 
范围限定运算符: [not] between 
字符比较运算符:[n
忽略符号: 
select....
检查值列表: 
select .....where
检查空值:where
检查选择表:wher
通过select-o
range语句) 
13、动态指定查询条
report Z_test. 
data:cond(
itab like table of cond, 
city1(10) value ’BEIJING’,
city1(10) value ’SINGAPORE
itab_spfli
concatenat
append cond to itab. 
concatenat
append cond to itab. 
select * into table itab_spfli
where (itab). 
14、多表结合查询(
reprot z_test. 
data: wa_carrid type spfli-carr
wa_connid type spfli-conn
wa_carrnam
select carrid connid 
from spfli into (wa_carrid
where cityform=’
select carrname from scarr into wa_carrnam
write wa_carrnam
endselect.
endselect.
15、for all entries选项 
reprot z_test. 
data: begin of wa_spfli, 
carrid type spfli-carr
connid type spfli-conn
end of wa_spfli, 
begin of wa_scarr, 
carrid type scarr-carr
carrname type scarr-carr
end of wa_scarr, 
spfli_tab like table of wa_spfli. 
select carrid connid 
from spfli 
into table spfli_tab 
where cityfrom =’Singapor
select carrid carrname 
from scarr 
into wa_scarr 
for all entires in spfli_tab 
where carrid = spfli_tab-
... 
endselect.
16、使用视图 
reprot z_test. 
data: wa_carrid type scarrspfli
wa_connid type scarrspfli
wa_carrnam
select carrid carrname connid 
from scarrspfli
into (wa_carrid
where cityfrom = ’Singapore
... 
endselect.
17、结合查询 
内连接:inner join 主表和结合表都满足o
左连接:left join 主选择表的数据,即使
report z_test. 
data:wa_ca
wa_connid type spfli-conn
wa_carrnam
select spfli-carr
from spfli 
inner join scarr on spfli-carr
into (wa_carrid
where spfli-city
..- 
endselect.
18、子查询(没有i
select .... 
from scarr 
into 
where exist (select * 
from spfli 
where carrid = scraa-carr
...where city in (select cityform from spfli where carrid = scarr-carr
...where city = (select cityform from spfli where carrid = scarr-carr
...where city > all (select cityform from spfli where carrid = scarr-carr
19、组合结果查询 
总计功能 
select carrid connid sum(seatso
from sflight 
into (wa_carrid
where spfli-city
分组统计: 
select carrid min (price) max(price)
into (carrid,mi
from sflight 
group by carrid 
write:/ carrid,min
endselect.
指定分组条件: 
select carrid min(price)
into(carri
from sflight 
group by carrid 
having min(minnum
指定行的顺序: 
select carrid connid max(seatso
into (wa_carrid
from sflight 
group by carrid 
order by carrid ascending max descending
20、使用表工作区:
声明:tables dbtab. 
tables spfli. 
... 
select single * from spfli wherer cityfrom =’Singapor
write:/ spfli-corr
21、动态指定数据库
dbname=’sp
select carrid connid 
from (dbname) into (carr_id,c
where cityfrom = ’Singapore
22、指定数据区域 
select * from spfli client specified into .... 
where mandt between ’100’ and ’103’. 
//从表spfli中
23、设置缓冲机制 
select....
使用distinct
会自动忽略缓冲。 
24、限定选择的行数
select ...from dbtab up to n rows.... 
25、操作性能分析 
report z_test. 
data:wa_ca
wa_connid type spfli-conn
wa_carrnam
data:t1 type i,t2 type i,time type i,n type i value 1000. 
do n times. 
get run time field t1. 
select carrid connid from spfli 
into (wa_carrid
select carrname from scarr 
into wa_carrnam
... 
endselect.
endselect.
get run time field t2. 
time = t2-t1. 
enddo. 
write :/ ’Runtime:’
26、使用数据库光标
report z_test. 
data: cur type cursor, 
wa_carrid type spfli-carr
wa_connid type spfli-conn
wa_cityfro
wa_cityto type spfli-city
start-of-s
open cursor cur for 
select carrid connid cityfrom cityto 
from spfli 
where carrid= ’AA’ 
order by carrid. 
... 
do. 
fetch next cursor cur 
into (wa_carrid
... 
if sy-subrc <> 0. 
close cursor cur. 
exit. 
endif. 
enddo. 
27、更新数据 
插入单行数据 
insert into dbtab values wa. 
insert into dbtab form wa. 
插入多行数据 
insert dbtab from table itab. 
更新单行数据 
update dbtab from wa. 
更新多行数据 
update dbtab set f1=g1...fi
update target from table itab.(从内表)
添加或更新单行 
modify dbtab from wa.(已存在则更新
添加或更新多行 
modify dbtab from table itab.(从内表)
删除单行数据 
delete from dbtab where <fix_key>.
delete from dbtab from wa. 
删除多行数据 
delete from dbtab where <condition
delete from [client specified]
删除所有数据 
.在通过内表删除多行
.使用where field like ’%’ 作为where子句中
28、数据库表的锁定
report z_test. 
data:wa_sf
wa_sflight
... 
call function ’ENQUEUE_E
EXPORTING 
mode_sflig
carrid = wa_sflight
connid = wa_sflight
fldate = wa_sflight
EXCEPTIONS
foreign_lo
system_fai
OTHERS =3. 
if sy-subrc <>0. 
message id sy-msgid type sy-msgty number sy-msgno 
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 
endif. 
update sflight set carrid = wa_sflight
call function ’’DEQUEUE_
29、程序中的授权检
report z_test. 
parameters
authority-
id ’CRRID’ field p_carrid 
id ’ACTVT’ field ’03’. 
if sy-subrc = 4. 
message e045(sabap
elseif sy-subrc <>0. 
message a888(sabap
endif. 
30、应用服务器文件
report z_test. 
parameters
data: wa_sflight
sflight_ta
sflight_ta
open dataset file for output in binary mode. 
select * from sflight into wa_sflight
transfer wa_sflight
append wa_sflight
endselect.
close dataset file. 
open dataset file for input in binary mode. 
do. 
read dataset file into wa_sflight
if sy-subrc <> 0. 
exit. 
endif. 
append wa_sflight
enddo. 
close dataset file. 
if sfilght_ta
message i888(sabap
endif. 
31、展示服务器文件
report z_test. 
parameters
ftype type rlgra-file
data: 
sflight_ta
sflight_ta
tab_line like line of sflight_ta
leng type i, 
lins type i, 
size type i. 
select * from sflight into table sflight_ta
describe field tab_line lenght leng. 
describe table sflight_ta
size = leng * lins. 
call function ’WS_DOWNLO
exporting 
filename=f
filetype=f
bin_filesi
tables 
data_tab=s
exceptions
... 
if sy-subrc <>0 
message e888(sabap
endif. 
call function ’WS_UPLOAD
exporting 
file
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号