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



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

Answer / 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

More DB2 Interview Questions

Can SQL statements/queries be included in a copybook?

2 Answers  


Why cursor is used in db2?

0 Answers  


How is the SUBSTR keyword used in sql?

1 Answers  


select distinct(empid),distinct(dept),name from EMP will the above query work?

2 Answers   CTS,


pls explain the precompilation process of cobol-db2 pgm

3 Answers   HCL, IBM,






What is a storage group (stogroup)?

0 Answers  


What is the latest version of ibm db2?

0 Answers  


What is the difference between SYNONYM and ALIAS?

6 Answers   TCS,


What do you do to keep the cursor from closing after a commit?

1 Answers  


Is schema the same as database?

0 Answers  


How do I copy a table in db2?

0 Answers  


What if , we failed to mentioed null indicator in sql select query , that may retrieve null value ?

2 Answers   Cap Gemini,


Categories