在Oracle,SQLServer中游标的使用是经常的,所以在ABAP不懂是不行的......
1、声明游标
OPEN CURSOR [WITH HOLD] <c> FOR SELECT <result>
FROM <source>
[WHERE <condition>]
[GROUP BY <fields>]
[HAVING <cond>]
[ORDER BY <fields>].
cursor <c>需事先在data语句中声明变量,在句型中可见,基本上select使用的所有附加项都可使用,除了一个,single
2、操作游标
向下移动游标:FETCH NEXT CURSOR <c> INTO <target>.
关闭游标:CLOSE CURSOR c1.
如:FETCH NEXT CURSOR S_CURSOR into TABLE itab PACKAGE SIZE S_S_IF-MAXSIZE.
FETCH NEXT CURSOR s_cursor into wa.
REPORT ydemo_rick_4.
DATA: c1 TYPE cursor, "声明游标 c2 TYPE cursor.
DATA: wa1 TYPE spfli, wa2 TYPE spfli.
DATA: flag1(1) TYPE c, flag2(1) TYPE c.
OPEN CURSOR: c1 FOR SELECT carrid connid FROM spfli WHERE carrid = 'LH',
c2 FOR SELECT carrid connid cityfrom cityto FROM spfli WHERE carrid = 'AZ'. DO. IF flag1 NE 'X'. FETCH NEXT CURSOR c1 INTO CORRESPONDING FIELDS OF wa1. "放到一个工作区中 IF sy-subrc <> 0. "当没数据时就关闭游标 CLOSE CURSOR c1. flag1 = 'X'. ELSE. WRITE: / wa1-carrid, wa1-connid. ENDIF. ENDIF.
IF flag2 NE 'X'. FETCH NEXT CURSOR c2 INTO CORRESPONDING FIELDS OF wa2. IF sy-subrc <> 0. CLOSE CURSOR c2. flag2 = 'X'. ELSE. WRITE: / wa2-carrid, wa2-connid, wa2-cityfrom, wa2-cityto. ENDIF. ENDIF. IF flag1 = 'X' AND flag2 = 'X'. EXIT. ENDIF. ENDDO.