在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.
下几种情况游标会知动关闭
第一、执行commit work和rollback work时。
第二、当执行OpenSql数据库提交或取消时。
第三、更改屏幕。
另,如果在open cursor时用了with hold, openSql在向数据库提交时游标不会自动关闭。
——————————————————————————————–
动态操作数据表的游标使用:
REPORT zles_tab_op_job. FIELD-SYMBOLS: <gs_fs>,<gt_fs> TYPE STANDARD TABLE."动态工作区,內表字段符号 DATA: gs_ref TYPE REF TO data, "动态工作区,內表引用 gt_ref TYPE REF TO data. DATA: lv_cursor TYPE cursor, lv_mes TYPE char20. DATA: lr_execption TYPE REF TO cx_root, lv_error TYPE string. PARAMETERS: p_tab TYPE tabname, p_sql TYPE string, p_num TYPE i, p_mode. CREATE DATA gt_ref TYPE TABLE OF (p_tab). ASSIGN gt_ref->* TO <gt_fs>. CREATE DATA gs_ref LIKE LINE OF <gt_fs>. ASSIGN gs_ref->* TO <gs_fs>. OPEN CURSOR WITH HOLD lv_cursor FOR SELECT * FROM (p_tab) WHERE (p_sql). DO. CLEAR <gt_fs>. TRY . FETCH NEXT CURSOR lv_cursor INTO TABLE <gt_fs> PACKAGE SIZE p_num. IF sy-subrc NE 0. CLOSE CURSOR lv_cursor. EXIT. ENDIF. CATCH cx_sy_open_sql_db INTO lr_execption. lv_error = lr_execption->get_text( ). IF lv_error IS NOT INITIAL. MESSAGE e000(oo) WITH lv_error. ENDIF. ENDTRY. CLEAR lv_mes. CASE p_mode. WHEN 'I'. INSERT (p_tab) FROM TABLE <gt_fs>. lv_mes = '插入'. WHEN 'D'. DELETE (p_tab) FROM TABLE <gt_fs>. lv_mes = '删除'. WHEN 'M'. MODIFY (p_tab) FROM TABLE <gt_fs>. lv_mes = '修改'. WHEN 'U'. UPDATE (p_tab) FROM TABLE <gt_fs>. lv_mes = '更新'. * WHEN gs_9000-query. * PERFORM frm_alv_display."ALV查询 ENDCASE. IF sy-subrc EQ 0. CALL FUNCTION 'DB_COMMIT'. * WRITE: '第',sy-index,'批,执行成功'. ELSE. CALL FUNCTION 'DB_ROLLBACK'. WRITE: / '第',sy-index,'批,',lv_mes,'失败'. ENDIF. ENDDO. CLOSE CURSOR lv_cursor.
View Code
注意:当使用
CALL FUNCTION ‘DB_COMMIT’. 数据提交
CALL FUNCTION ‘DB_ROLLBACK’. 数据回滚
不会关闭游标!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18371.html