View previous topic :: View next topic |
Author |
Message |
Jamylady Beginner
Joined: 04 Nov 2004 Posts: 68 Topics: 22
|
Posted: Thu Sep 10, 2009 10:13 am Post subject: Cursor picks up the record just inserted during the fetch |
|
|
Hi,
I have a requirement for a new program which requires us to select from a cursor and insert into the same table later in the program if the record requirements are correct.
Looks like the cursor picks up the records that are inserted during the program execution. (I wasnt aware of this or I thought this would never happen), Is there any way to prevent this? I am doing the bind with CS (Cursor Stebility)option.
Then my solution has been to create a subscripted temporary table in working storage and then at end of cursor/program insert the records held on the temporary table.
I have come across another solution - Use of Temp DB2 tables, not sure about the performance though
Do you know of a better way of doing this?
Thanks
ja |
|
Back to top |
|
 |
jim haire Beginner
Joined: 30 Dec 2002 Posts: 140 Topics: 40
|
Posted: Thu Sep 10, 2009 1:25 pm Post subject: |
|
|
I don't know if the records you are inserting have an update timestamp field, but one way this can be done is to capture the current timestamp before you do any inserts and then build your cursor so that it has additional filter where you only retrieve rows that are less than or equal to the current timestamp.
Any new rows inserted would have timestamps greater than the current timestamp. In this way these rows would not get picked up. |
|
Back to top |
|
 |
jim haire Beginner
Joined: 30 Dec 2002 Posts: 140 Topics: 40
|
Posted: Thu Sep 10, 2009 1:28 pm Post subject: |
|
|
A little clearer version of the solution above:
I don't know if the records you are inserting have an update timestamp field, but one way this can be done is to capture the current timestamp before you do any inserts and then build your cursor so that it has additional filter where you only retrieve rows that are less than or equal to the timestamp you had captured.
Any new rows inserted would have timestamps greater than the timestamp you had captured. In this way these rows would not get picked up. |
|
Back to top |
|
 |
Jamylady Beginner
Joined: 04 Nov 2004 Posts: 68 Topics: 22
|
Posted: Fri Sep 11, 2009 4:47 am Post subject: |
|
|
Hi Jim
That is a good idea, but we dont have a time stamp filed on the row to capure the updated time:(
Thank you |
|
Back to top |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Fri Sep 11, 2009 7:13 am Post subject: |
|
|
look at the appl programmers guide and the reference manual for your version of DB2,
keyword: sensitive _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
 |
Jamylady Beginner
Joined: 04 Nov 2004 Posts: 68 Topics: 22
|
Posted: Fri Sep 11, 2009 10:41 am Post subject: |
|
|
Great. Thanks
It worked
Code: |
INSENSITIVE
Specifies that the cursor does not have sensitivity to
inserts, updates, or deletes that are made to the rows
underlying the result table. As a result, the size of the
result table, the order of the rows, and the values for each
row do not change after the cursor is opened. In addition, the
cursor is read-only. The SELECT statement or attribute-string
of the PREPARE statement cannot contain a FOR UPDATE clause,
and the cursor cannot be used for positioned updates or
deletes.
|
I used the following example Code: |
EXEC SQL DECLARE C1 INSENSITIVE SCROLL CURSOR FOR
SELECT DEPTNO, DEPTNAME, MGRNO
FROM DSN8810.DEPT
ORDER BY DEPTNO
END-EXEC.
|
Thank you very much
JA |
|
Back to top |
|
 |
jctgf Beginner
Joined: 05 Nov 2006 Posts: 89 Topics: 36
|
Posted: Mon Sep 28, 2009 9:03 am Post subject: |
|
|
Hi,
I don't think it's a good idea to insert lines in a db2 table in batch mode with embedded Sql.
The problem is that if the program abends, you'll have your db2 table partially populated.
If you restart your program, you'll have to mind about the records already inserted.
I'd simply write all the records to be inserted in a SAM file and then load it into the table (LOAD utility) when the program ends.
Actually it would be even way faster.
Just an opinion. |
|
Back to top |
|
 |
Jamylady Beginner
Joined: 04 Nov 2004 Posts: 68 Topics: 22
|
Posted: Tue Mar 23, 2010 3:04 pm Post subject: |
|
|
Hi jctgf,
What if I control the program abends and issue a ROLLBACK? I think my program is managing the program abends and rolling back all the table inserts/updates.
Thanks
JA |
|
Back to top |
|
 |
papadi Supermod
Joined: 20 Oct 2009 Posts: 594 Topics: 1
|
Posted: Tue Mar 23, 2010 3:21 pm Post subject: |
|
|
Quote: |
I don't think it's a good idea to insert lines in a db2 table in batch mode with embedded Sql. | Opinions drastically vary. . . It is a perfectly good idea to do inserts in a batch program.
Quote: |
The problem is that if the program abends, you'll have your db2 table partially populated.
If you restart your program, you'll have to mind about the records already inserted. | Not a problem for most situations. . .
Quote: |
Actually it would be even way faster. | Not necessarily. . . It there are to be millions of rows inserted in a single process, yes, an external load would "run faster". If there a modest volume of data to be inserted, doing this within the code works quite well. _________________ All the best,
di |
|
Back to top |
|
 |
|
|