Social Icons

twitter email

Tuesday, July 8, 2014

How to UPDATE and DELETE rows in a cursor controlled result table

If in a COBOL program we need to UPDATE or DELETE rows using a cursor-controlled result table, then we use DECLARE CURSOR and OPEN CURSOR statements to open a cursor-controlled table. Next, we use FETCH statement to move through the rows in the table. Then when we reach a row that we want to update or delete, we issue an UPDATE or DELETE statement statement to perform a positioned update or delete.
Consider the below examples :-

A DECLARE CURSOR statement for a customer table is given below -
EXEC SQL
  DECLARE CUSTCURS CURSOR WITH HOLD FOR
      SELECT CUSTNO, FNAME, LNAME, CITY, STATE
      FROM MM01.CUSTOMER
      WHERE INVCUST < '100000' 
      FOR UPDATE OF FNAME, LNAME, CITY, STATE
END-EXEC.
An UPDATE statement that changes the row at the current cursor position -
EXEC SQL
    UPDATE MM01.CUSTOMER
          SET FNAME = :FNAME
              LNAME = :LNAME
              CITY  = :CITY
              STATE = :STATE
          WHERE CURRENT OF CUSTCURS
END-EXEC.
A DELETE statement that removes the row at the current cursor position -
EXEC SQL
    DELETE FROM MM01.CUSTOMER
          WHERE CURRENT OF CUSTCURS
END-EXEC.
The WITH HOLD phrase that is coded in the DECLARE CURSOR statement in above example prevents the cursor from being closed when a commit operation takes place.

Restrictions on UPDATE and DELETE statements :-
  1. We cant update or delete a row in read-only table
  2. A cursor-controlled table is a read-only table when it uses those DB2 features that are incompatible with updating or deleting. For example, a SELECT statement within a DECLARE CURSOR statement uses the DISTINCT keyword, its resukt table is a read-only.
  3. A cursor controlled table is read-only , when the SELECT statement in the DECLARE CURSOR statement sues a UNION, some categories of subqueries, a column function or any of these keywords : DISTINCT, ORDER BY, GROUP BY and HAVING.

No comments:

Post a Comment

 
Blogger Templates