2007 Jul 07 1:39 PM
Hi All,
Which of the following usages, is more efficient and why?
1st method: Using OPEN CURSOR
OPEN CURSOR WITH HOLD I_CURSOR FOR
SELECT (FIELDS) FROM (TABLE)
WHERE (WHERE_TAB).
DO.
FETCH NEXT CURSOR I_CURSOR
APPENDING TABLE I_DATA_TABLE
PACKAGE SIZE I_MAXIMUM_LINES.
IF SY-SUBRC NE 0.
EXIT.
ENDIF.
<<<<<<<<<<<<<<<Other processing for selected data>>>>>>>>>>>
ENDDO.
2nd method: Using SELECT...ENDSELECT
SELECT (FIELDS)
PACKAGE SIZE I_MAXIMUM_LINES.
FROM (TABLE)
APPENDING TABLE I_DATA_TABLE
WHERE (WHERE_TAB).
<<<<<<<<<<<<<<<Other processing for selected data>>>>>>>>>>>
ENDSELECT.
I feel that the second method, has less lines of code, hits the database same number of times as the first one, is easier to understand. So, I get a feeling that second method is better than the first one. But, most of the SAP standard programs I see, use the first method, which is contradicts my thoughts.
Expert comments please.....
Thanks,
Juwin.
Hi All,
Which of the following usages, is more efficient and why?
1st method: Using OPEN CURSOR
OPEN CURSOR WITH HOLD I_CURSOR FOR
SELECT (FIELDS) FROM (TABLE)
WHERE (WHERE_TAB).
DO.
FETCH NEXT CURSOR I_CURSOR
APPENDING TABLE I_DATA_TABLE
PACKAGE SIZE I_MAXIMUM_LINES.
IF SY-SUBRC NE 0.
EXIT.
ENDIF.
<<<<<<<<<<<<<<<Other processing for selected data>>>>>>>>>>>
ENDDO.
2nd method: Using SELECT...ENDSELECT
SELECT (FIELDS)
PACKAGE SIZE I_MAXIMUM_LINES.
FROM (TABLE)
APPENDING TABLE I_DATA_TABLE
WHERE (WHERE_TAB).
<<<<<<<<<<<<<<<Other processing for selected data>>>>>>>>>>>
ENDSELECT.
I feel that the second method, has less lines of code, hits the database same number of times as the first one, is easier to understand. So, I get a feeling that second method is better than the first one. But, most of the SAP standard programs I see, use the first method, which is contradicts my thoughts.
Expert comments please.....
Thanks,
Juwin.
2007 Jul 07 1:54 PM
Hi Juwin,
The first method is getting data using NATIVE SQL which is not used anymore.
The standard code uses that, because they are very old code,written long back,.
Regards,
Atish
2007 Jul 07 1:57 PM
Hi
Select and endselect is treated as a loop.
Open: opening the sql
Fetch: fetch the data from database
Close: close the opened sql
for more information check the following link:
Reward al helpfull answers
Regards
Pavan
2007 Jul 07 4:13 PM
Hello Juwin,
following lines traced by st05 will show no difference
tables: marc.
select single * from marc.
select * from marc up to 1 rows.
endselect.
Here is the select single:
MARC PREPARE 0 0 SELECT WHERE "MANDT" = :A0 AND ROWNUM <= :A1
MARC OPEN 0 0 SELECT WHERE "MANDT" = '400' AND ROWNUM <= 1
MARC FETCH 1 0
Here goes the up to 1 rows:
MARC REOPEN 0 0 SELECT WHERE "MANDT" = '400' AND ROWNUM <= 1
MARC FETCH 1 0
So if in both cases on database level the same select statement is used how should the behaviour different?
I would suggest that don't use both query's and select endselect - is major performance issue when you test in production.
Open cursor is outdated.
Thanks
Seshu
2007 Jul 09 1:56 PM
Hi,
I think people misunderstood my SELECT...ENDSELECT statement.
Please note that, in my SELECT...ENDSELECT, I am using the addition PACKAGE SIZE I_MAXIMUM_LINES.
FETCH is also inside a DO... ENDDO loop, and also uses PACKAGE SIZE I_MAXIMUM_LINES. So, both of them will hit the database, same number of times.
Logical databases defined by SAP, also uses OPEN...FETCH, logic.
Please correct me if I am wrong.
Thanks,
Juwin.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |