Skip to Content
1
Former Member
Nov 21, 2013 at 01:14 PM

Generic Datasource - open cursor, close cursor problem.

2111 Views

Hi Experts,

I tried to cretae the Generic Datasource through Functional module. After creating the Datasource i tried to execute in RSA3, the below said runtime error is coming.

Short text
Cursor already closed or not yet open.

What happened?
Error in the ABAP Application Program

The current ABAP program "SAPLZBW_FG" had to be terminated because it has
come across a statement that unfortunately cannot be executed.

Error analysis
An exception occurred that is explained in detail below.
The exception, which is assigned to class 'CX_SY_OPEN_SQL_DB', was not caught
in
procedure "ZFREIGHTRATE" "(FUNCTION)", nor was it propagated by a RAISING
clause.
Since the caller of the procedure could not have anticipated that the
exception would occur, the current program is terminated.
The reason for the exception is:
The cursor used in a FETCH or CLOSE CURSOR command is
not open. It was either not yet open or has already
been closed. You can close the cursor explicitly with
the CLOSE CURSOR command, implicitly with the COMMIT WORK command,
or with a screen change.

*************************************************************************************************************************

Please see the below source code which i had written. please correct me where i had gone wrong

FUNCTION ZFREIGHTRATE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(I_REQUNR) TYPE SRSC_S_IF_SIMPLE-REQUNR
*" VALUE(I_DSOURCE) TYPE SRSC_S_IF_SIMPLE-DSOURCE OPTIONAL
*" VALUE(I_MAXSIZE) TYPE SRSC_S_IF_SIMPLE-MAXSIZE OPTIONAL
*" VALUE(I_INITFLAG) TYPE SRSC_S_IF_SIMPLE-INITFLAG OPTIONAL
*" VALUE(I_READ_ONLY) TYPE SRSC_S_IF_SIMPLE-READONLY OPTIONAL
*" VALUE(I_REMOTE_CALL) TYPE SBIWA_FLAG DEFAULT SBIWA_C_FLAG_OFF
*" TABLES
*" I_T_SELECT TYPE SRSC_S_IF_SIMPLE-T_SELECT OPTIONAL
*" I_T_FIELDS TYPE SRSC_S_IF_SIMPLE-T_FIELDS OPTIONAL
*" E_T_DATA STRUCTURE ZFRERATE OPTIONAL
*" EXCEPTIONS
*" NO_MORE_DATA
*" ERROR_PASSED_TO_MESS_HANDLER
*"----------------------------------------------------------------------

* Example: DataSource for table SFLIGHT
TABLES: A651.
TABLES: ZFRERATE.

* Auxiliary Selection criteria structure
DATA: L_S_SELECT TYPE SRSC_S_SELECT.

* Maximum number of lines for DB table
STATICS: S_S_IF TYPE SRSC_S_IF_SIMPLE,

* counter
S_COUNTER_DATAPAKID LIKE SY-TABIX,

* cursor
S_CURSOR TYPE CURSOR.
* Select ranges
RANGES: L_R_KSCHL FOR A651-KSCHL,
L_R_TDLNR FOR A651-TDLNR,
L_R_TPLST FOR A651-TPLST,
L_R_VSART FOR A651-VSART,
L_R_LOEVM_KO FOR KONP-LOEVM_KO.

DATA: BEGIN OF T_A651 occurs 0,
KSCHL TYPE KSCHL,
TPLST TYPE TPLST,
TDLNR TYPE TDLNR,
VSART TYPE VSART,
VSBED TYPE VSBED,
DATBI TYPE DATBI,
ROUTE TYPE ROUTE,
DATAB TYPE DATAB,
KNUMH TYPE KNUMH,
END OF T_A651.


DATA: BEGIN OF T_KONP occurs 0,
KSCHL TYPE KSCHL,
* TPLST TYPE TPLST,
* TDLNR TYPE TDLNR,
* VSART TYPE VSART,
* VSBED TYPE VSBED,
* DATBI TYPE DATBI,
* ROUTE TYPE ROUTE,
* DATAB TYPE DATAB,
KNUMH TYPE KNUMH,
LOEVM_KO TYPE LOEVM_KO,
KBETR TYPE KBETR,
KONWA type KONWA,
END OF T_KONP.

* Initialization mode (first call by SAPI) or data transfer mode
* (following calls) ?
IF I_INITFLAG = SBIWA_C_FLAG_ON.

************************************************************************
* Initialization: check input parameters
* buffer input parameters
* prepare data selection
************************************************************************

* Check DataSource validity
CASE I_DSOURCE.
WHEN 'ZSD_FREIGHT'.
WHEN OTHERS.
IF 1 = 2. MESSAGE E009(R3). ENDIF.
* this is a typical log call. Please write every error message like this
LOG_WRITE 'E' "message type
'R3' "message class
'009' "message number
I_DSOURCE "message variable 1
' '. "message variable 2
RAISE ERROR_PASSED_TO_MESS_HANDLER.
ENDCASE.

APPEND LINES OF I_T_SELECT TO S_S_IF-T_SELECT.

* Fill parameter buffer for data extraction calls
S_S_IF-REQUNR = I_REQUNR.
S_S_IF-DSOURCE = I_DSOURCE.
S_S_IF-MAXSIZE = I_MAXSIZE.

* Fill field list table for an optimized select statement
* (in case that there is no 1:1 relation between InfoSource fields
* and database table fields this may be far from beeing trivial)
APPEND LINES OF I_T_FIELDS TO S_S_IF-T_FIELDS.

ELSE. "Initialization mode or data extraction ?

************************************************************************
* Data transfer: First Call OPEN CURSOR + FETCH
* Following Calls FETCH only
************************************************************************

* First data package -> OPEN CURSOR
IF S_COUNTER_DATAPAKID = 0.

* Fill range tables BW will only pass down simple selection criteria
* of the type SIGN = 'I' and OPTION = 'EQ' or OPTION = 'BT'.
LOOP AT S_S_IF-T_SELECT INTO L_S_SELECT WHERE FIELDNM = 'KSCHL'.
MOVE-CORRESPONDING L_S_SELECT TO L_R_KSCHL.
APPEND L_R_KSCHL.
ENDLOOP.

LOOP AT S_S_IF-T_SELECT INTO L_S_SELECT WHERE FIELDNM = 'TDLNR'.
MOVE-CORRESPONDING L_S_SELECT TO L_R_TDLNR.
APPEND L_R_TDLNR.
ENDLOOP.

LOOP AT S_S_IF-T_SELECT INTO L_S_SELECT WHERE FIELDNM = 'TPLST'.
MOVE-CORRESPONDING L_S_SELECT TO L_R_TPLST.
APPEND L_R_TPLST.
ENDLOOP.

LOOP AT S_S_IF-T_SELECT INTO L_S_SELECT WHERE FIELDNM = 'VSART'.
MOVE-CORRESPONDING L_S_SELECT TO L_R_VSART.
APPEND L_R_VSART.
ENDLOOP.

LOOP AT S_S_IF-T_SELECT INTO L_S_SELECT WHERE FIELDNM = 'LOEVM_KO'.
MOVE-CORRESPONDING L_S_SELECT TO L_R_LOEVM_KO.
APPEND L_R_LOEVM_KO.
ENDLOOP.
* Determine number of database records to be read per FETCH statement
* from input parameter I_MAXSIZE. If there is a one to one relation
* between DataSource table lines and database entries, this is trivial.
* In other cases, it may be impossible and some estimated value has to
* be determined.
OPEN CURSOR WITH HOLD S_CURSOR FOR

SELECT KSCHL TPLST TDLNR VSART VSBED DATBI ROUTE DATAB KNUMH FROM A651
WHERE KSCHL IN L_R_KSCHL AND
TDLNR IN L_R_TDLNR AND
TPLST IN L_R_TPLST AND
VSART IN L_R_VSART.

ENDIF.
ENDIF.

"First data package ?

* Fetch records into interface table.
* named E_T_'Name of extract structure'.
FETCH NEXT CURSOR S_CURSOR
APPENDING CORRESPONDING FIELDS
OF TABLE E_T_DATA
PACKAGE SIZE S_S_IF-MAXSIZE.


IF SY-SUBRC <> 0.
CLOSE CURSOR S_CURSOR.
RAISE NO_MORE_DATA.
ENDIF.

IF E_T_DATA[] IS NOT INITIAL.

SELECT KSCHL KNUMH LOEVM_KO KBETR FROM KONP INTO TABLE T_KONP FOR ALL ENTRIES IN E_T_DATA
WHERE KSCHL = E_T_DATA-KSCHL AND
KNUMH = E_T_DATA-KNUMH.

LOOP AT E_T_DATA.
READ TABLE T_KONP WITH KEY KSCHL = E_T_DATA-KSCHL
KNUMH = E_T_DATA-KNUMH.

IF SY-SUBRC = 0.
E_T_DATA-LOEVM_KO = T_KONP-LOEVM_KO.
E_T_DATA-KBETR = T_KONP-KBETR.
E_T_DATA-KONWA = T_KONP-KONWA.
APPEND E_T_DATA.
ENDIF.


ENDLOOP.

IF L_R_LOEVM_KO = 'x'.

LOOP AT E_T_DATA.
DELETE E_T_DATA WHERE LOEVM_KO = ''.

ENDLOOP.

* MODIFY E_T_DATA.

ENDIF.


S_COUNTER_DATAPAKID = S_COUNTER_DATAPAKID + 1.

ENDIF. "Initialization mode or data extraction ?

ENDFUNCTION.