ROWSET processing is a group of rows for the result set of a query that is returned by a single FETCH statement. You can control the number of rows you want to fetch and return by specifying rowset limit in your program. ROWSET size is specified on the FETCH statement, the maximum rowset size is 32,767. This Can significantly improve performance as large amounts of data are either retrieved or inserted into DB2 tables at one go.
Multi-row FETCH: A single FETCH statement can retrieve multiple rows of data from the result table of a query as a rowset. DECLARE CURSOR adds a new “WITH ROWSET POSITIONING” clause. Row positioning FETCH statements may be used with cursor declared with the “WITH ROWSET POSITIONING” clause, or the “WITHOUT ROWSET POSITIONING” clause. Rowset positioning FETCH statements may only be used with cursors declared with the “WITH ROWSET POSITIONING” clause.
Your program needs to be written to include arrays that will work with multi-row SQL operations.
01 OUTPUT-VARS. 05 LASTNME OCCURS 10 TIMES. 49 LASTNME-LGTH PIC S9(4) USAGE COMP. 49 LASTNME-DATA PIC X(50). 05 EMPNO PIC S9(9) COMP-4 OCCURS 10 TIMES.
To enable a cursor to fetch rowsets, use the WITH ROWSET POSITIONING clause in the DECLARE CURSOR statement.
EXEC SQL DECLARE C1 CURSOR WITH ROWSET POSITIONING FOR SELECT LASTNME, EMPNO FROM EMP END-EXEC.
EXEC SQL OPEN C1 END-EXEC.
Rowset positioning specifies whether multiple rows of data can be accessed as a rowset on a single FETCH statement – default is WITHOUT ROWSET POSITIONING. Fetch the first x rows and leave the cursor positioned on that rowset at the completion of the fetch. FOR n ROWS with FETCH FIRST n ROWS ONLY these two clauses may be used together. FETCH FIRST n ROWS ONLY dominates.
EXEC SQL FETCH FIRST ROWSET FROM C1 FOR 50 ROWS INTO :LASTNME,:EMPNO END-EXEC. EXEC SQL FETCH NEXT ROWSET FROM C1 FOR 50 ROWS INTO :LASTNME,:EMPNO END-EXEC.
For example, if there are 180 rows being fetched and the rowset size is 50 so in ROWSET processing:
Fetch 1 will get a zero SQLCODE and the SQLERRD(3) will be 50
Fetch 2 will get a zero SQLCODE and the SQLERRD(3) will be 50
Fetch 3 will get a zero SQLCODE and the SQLERRD(3) will be 50
Fetch 4 will get a zero SQLCODE and the SQLERRD(3) will be 30
If you try to fetch beyond the result set you will receive an end of data condition.
The current “sweet spot” for multi-row FETCH is at about 100 rows. Performance gains start with 10 rows; if you are going to retrieve less than that it may not make a lot of sense to code multi-row FETCH.
How to fetch the specified number of rows regardless of the current position of cursor: Fetch 5 rows starting with row 10 regardless of the current position of the cursor and cause the cursor to be positioned on that rowset at the completion of the fetch.
FETCH ROWSET STARTING AT ABSOLUTE 10 FROM C1 FOR 5 ROWS INTO …
If FOR n ROWS is NOT specified and the cursor is declared for ROWSET positioning. The ROWSET will be the same as the previous ROWSET FETCH if…
A single INSERT statement can add multiple rows of data from an array. For static and dynamic SQL (examples upcoming) FOR n ROWS. Maximum n is 32,767.
ATOMIC – if any single row insert fails, all fail
NOT ATOMIC – rows inserted independently. NOT ATOMIC CONTINUE ON SQLEXCEPTION allows some rows to be successfully inserted into a table and failing rows to be indicated for possible further processing (For example, SQLCODE -803). GET DIAGNOSTICS should be used to determine the failing rows.
Static SQL INSERT with host variable arrays.
INSERT INTO EMP FOR n ROWS VALUES(:LASTNME,:EMPNO) ATOMIC
Update a specified row in the rowset:
UPDATE T1 SET C1 = 5 WHERE CURRENT OF cursor-name FOR ROW row-number OF ROWSET
Instead, if you specify the existing WHERE CURRENT OF cursor-name, all the rows in the rowset are updated. For example: Update all the rows in the rowset that cursor CSR1 is positioned on.
UPDATE T1 SET C1 = 5 WHERE CURRENT OF CSR1
Following clause to delete a specified row in the rowset processing:
DELETE FROM T1 WHERE CURRENT OF cursor-name FOR ROW row-number OF ROWSET
Instead, if you specify the existing WHERE CURRENT OF cursor name, all the rows in the rowset are deleted. The following example deletes the fourth row in the rowset that cursor CSR1 is positioned on:
DELETE FROM T1 WHERE CURRENT OF CSR1 FOR ROW 4 OF ROWSET
A well-maintained product backlog is crucial for successful product development. It serves as a single…
Incremental value to the customer refers to the gradual delivery of small, functional parts of…
A Product Market refers to the group of potential customers who might be interested in…
The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…
The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…
Choosing the right Scrum Master Certification depends on your current experience and career goals. If…