Is it Possible to declare or create a cursor for UPDATE of
table? If yes tell me how? If no Tell me why?

Answer Posted / lu

yes, read this :
Updating a column: You can update columns in the rows that
you retrieve. Updating a row after you use a cursor to
retrieve it is called a positioned update. If you intend to
perform any positioned updates on the identified table,
include the FOR UPDATE clause. The FOR UPDATE clause has
two forms:
• The first form is FOR UPDATE OF column-list. Use
this form when you know in advance which columns you need
to update.
• The second form is FOR UPDATE, with no column list.
Use this form when you might use the cursor to update any
of the columns of the table.
For example, you can use this cursor to update only the
SALARY column of the employee table:

EXEC SQL
DECLARE C1 CURSOR FOR
SELECT EMPNO, FIRSTNME, MIDINIT, LASTNAME, SALARY
FROM DSN8810.EMP X
WHERE EXISTS
(SELECT *
FROM DSN8810.PROJ Y
WHERE X.EMPNO=Y.RESPEMP
AND Y.PROJNO=:GOODPROJ)
FOR UPDATE OF SALARY;

If you might use the cursor to update any column of the
employee table, define the cursor like this:

EXEC SQL
DECLARE C1 CURSOR FOR
SELECT EMPNO, FIRSTNME, MIDINIT, LASTNAME, SALARY
FROM DSN8810.EMP X
WHERE EXISTS
(SELECT *
FROM DSN8810.PROJ Y
WHERE X.EMPNO=Y.RESPEMP
AND Y.PROJNO=:GOODPROJ)
FOR UPDATE;

DB2 must do more processing when you use the FOR UPDATE
clause without a column list than when you use the FOR
UPDATE clause with a column list. Therefore, if you intend
to update only a few columns of a table, your program can
run more efficiently if you include a column list.
The precompiler options NOFOR and STDSQL affect the use of
the FOR UPDATE clause in static SQL statements. If you do
not specify the FOR UPDATE clause in a DECLARE CURSOR
statement, and you do not specify the STDSQL(YES) option or
the NOFOR precompiler options, you receive an error if you
execute a positioned UPDATE statement.

Is This Answer Correct ?    18 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the full forms of spufi and dclgen and why are they used?

654


How to find the maximum value in a column in the db2 database?

601


Hi Everyone... Under the Logical files, when am working on multiple rec format logical file, I have joined two pf's using the multiple record format logical file concept, but when i run queried the LF only the first mentioned pf records are reflecting and I am not able to see any of the field records specified under the second pf. Kindly let me know whats the reason behind this. Below is the str of LF., R rec PFILE(LOGICAA) CUSTNO CUSTNAME BILL K CUSTNO R rec1 PFILE(LOGICAA1) CUSTNO ADD K CUSTNO .....

1889


can all users have the privilage to use the sql statement select * (dml)?

637


What is the physical storage length of the data types date, time, and timestamp?

600






I HAVE 500 ROW TO UPDATE I WOULD LIKE TO USE ROLLBACK ALONG WITH COMMIT.WHAT IS THE SYNTAX TO CODE COMMIT AND ROLLBACK FOR EVERY 100 ROWS.AND HOW THE CURSOR ROLLBACK TO THE LAST COMMITTING POINT.

2753


Why do we use cursor?

594


How can you find out the # of rows updated after an update statement?

634


run jcl for cobol+db2 pgm?.. //jobcard //step001 exec pgm=ikjeft01 //systin dd * DSNSYSTEM(--------) RUN PROGRAM(MYPGMNAME) PLAN(-----), LIB(-------), PARMS(------) /* WHAT WILL U GIVE INSIDE THE BRACKETS... WHAT IS PLAN,PACKAGE,BIND?..PLAN N PACAKGES ARE GENERATED BY ?...

3784


What is the usage of open cursor command?

599


What is buffer pool?

607


How do I delete a table in db2?

642


Give a brief description of db2 isolation levels?

599


How do I import data from excel to db2?

607


What is host variable?

590