SAP Commit within ABAP Select Endselect using cursor fetch

Advertisements

Have you ever wanted to perform an ABAP Commit within ABAP Select Endselect statement. iF so you will have found that within a SELECT ENDSELECT you can not trigger an explicit commit otherwise you will get the DBIF_RSQL_INVALID_CURSOR error.

Advertisements

The problem is if you do want to perform a commit every x number of records you need to exit the select..endselect do the commit and then somehow return to the record you left off at. The following example ABAP code allows you to do this using a cursor and a fetch statement.

ABAP cursor fetch code to commit within select end select

*&———————————————————————*
*& Report ZSELECT_CURSOR
*&
*&———————————————————————*
*&
*&
*&———————————————————————*
REPORT zselect_cursor.

Advertisements

DATA: it_table TYPE STANDARD TABLE OF but000,
wa_table LIKE LINE OF it_table.

Advertisements

DATA: ld_cursor TYPE cursor.

Advertisements

PARAMETER: test AS CHECKBOX.

SELECT *
FROM but000
INTO wa_table.

” You can’t commit here within a select …endselect

ENDSELECT.

* Use OPEN cursor and fetch to be able to select records row by row
* or multiple rows at a time and perform a commit per fetch
OPEN CURSOR WITH HOLD ld_cursor FOR

SELECT * FROM but000.
DO.
FETCH NEXT CURSOR ld_cursor
INTO TABLE it_table PACKAGE SIZE 1000.
IF sy-subrc <> 0.
EXIT.
ENDIF.

* Do any updated here
” LOOP AT it_table into wa_table.
” modify…
” ENDLOOP.
“CALL FUNCTION ‘DB_COMMIT’.
“COMMIT WORK.

ENDDO.

CLOSE CURSOR ld_cursor.

Advertisements