Posted: Tue Jan 16, 2007 10:40 pm Post subject: Query on Cursors
Hi,
Given a cursor C1 on table T1. For every row fetched from T1, open a cursor C2 on table T2. For every row fetched from T2, update column C1 in table T2 and issue a COMMIT.
How can we define a Cursor C1 in this regard. Does "DECLARE c1 CURSOR WITH HOLD FOR SELECT * FROM t1" would do the job for us. Do we need to mention anything about the cursor C2 here ? Some where down in my heart, I feel we actually need not mention anything about Cursor C2, but would like to confirm about it.
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
Posted: Wed Jan 17, 2007 2:21 am Post subject:
hopefully your are doing a positioned update on cursor C1. To do so, requires a 'FOR UPDATE OF' clause.
suggest:
DECLARE C1 CURSOR WITH HOLD FOR SELECT A,B,C FROM T1 WHERE ... for UPDATE of C
where A,B,C are the necessary columns needed to declare cursor C2
and are updated - C- based on C2 results
would not use SELECT *
WITH HOLD will keep a cursor open after a COMMIT.
no, you do not mention the second cursor within either the DECLARE or FETCH of the first cursor.
you are only updating each row of C1 once?
COMMITs are very resource intensive. Unless your C2 Fetchs thousands of rows for each row of C1, I would increase my LUW and not COMMIT after every positioned cursor update of C1.
Have you made provisions for RESTART? _________________ Dick Brenholtz
American living in Varel, Germany
There is some confusion here. I think the update is on T2. So the 'WITH HOLD', 'FOR UPDATE' should be in C2 cursor.. I agree with Dick on his comments on Commit.
Note of caution: Hope that your 'update' cursor does not involve joins, ORDER BY, etc .. because UPDATE CURSOR wouldn't work in that case. Read this link for more,
But as I mentioned earlier "Given a cursor C1 on table T1. For every row fetched from T1, open a cursor C2 on table T2. For every row fetched from T2, update column C1 in table T2 and issue a COMMIT. "
For every row fetched from T1 : It means a cursor C1 needs to be opened for table T1 initially.
open a cursor C2 on table T2 : Open a Cursor C2 on table T2.
For every row fetched from T2, update column C1 in table T2 : That means using the cursor C2, and for every row we fetch we need to update column of Table T2.
Does it not mean that, irrespective of cursor C1 on Table 1, we need to have a cursor open on Table T2 and update its columns.
As Dick mentioned,
DECLARE C1 CURSOR WITH HOLD FOR SELECT A,B,C FROM T1 WHERE ... for UPDATE of C
where A,B,C are the necessary columns needed to declare cursor C2
and are updated - C- based on C2 results
what does "where A,B,C are the necessary columns needed to declare cursor C2" mean here. How can be the columns of Table T1 be used to declare the cursor C2 which is needed in Table T2. Can you elaborate on this.
Also Blitz, no joins are being used here.
Actually this is one of the questions, which I faced in a sample DB2 703 exam and following are the options given for the above question:
DECLARE c1 CURSOR WITH HOLD FOR SELECT * FROM t1
DECLARE c1 CURSOR WITH RETURN FOR SELECT * FROM t1
DECLARE c1 CURSOR FOR SELECT * FROM t1 FOR UPDATE OF t2
DECLARE c1 CURSOR FOR SELECT * FROM t1 FOR UPDATE OF c1
This may provide you more information.
I'm sure that I'm confused and also confusing you too . But Now I must have provided a better information.
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
Posted: Wed Jan 17, 2007 1:54 pm Post subject:
as blitz said, I got it backwords: the update is C2, no update in C1.
For your quiz CURSOR C1 is defined by:
DECLARE c1 CURSOR WITH HOLD FOR SELECT * FROM t1
Quote:
what does "where A,B,C are the necessary columns needed to declare cursor C2" mean here. How can be the columns of Table T1 be used to declare the cursor C2 which is needed in Table T2.
Since it is a school question, the 'SELECT *' format was used. I consider the 'SELECT *' format poor programing practice and wanted to say only columns from C1 should be selected that proved the necessary values from each row in C1 that is to be contained in variables used in the WHERE CLAUSE of the C2 declaration.
very poor wording on my part, I apologize. _________________ Dick Brenholtz
American living in Varel, Germany
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum