If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > temp tables - all rows get removed on duplicate insert

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-05-08, 06:16
andrewhallam andrewhallam is offline
Registered User
 
Join Date: Dec 2003
Location: Nottingham, England
Posts: 52
temp tables - all rows get removed on duplicate insert

DB2 7.2

Could anyone out there tell my how to stop DB2 removing all the rows from my temporary table when the unique constraint gets violated.

table:
DECLARE GLOBAL TEMPORARY table tt (a VARGRAPHIC(10),b VARGRAPHIC(20) ) ON COMMIT PRESERVE ROWS NOT LOGGED
index:
CREATE unique index SESSION.xtt on SESSION.tt(a)

inserts:
insert into SESSION.tt (a, b) values ('xxx', 'xxx')
insert into SESSION.tt (a, b) values ('yyy', 'yyy')

count:
select count(*) from SESSION.tt
- result is 2 - GOOD!

another insert (get duplicate error as expected):
insert into SESSION.tt (a, b) values ('xxx', 'xxx')
- gets [IBM][CLI Driver][DB2/LINUX] SQL0803N One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "1" constrains table "SESSION.TT" from having duplicate rows for those columns. SQLSTATE=23505

do count again:
select count(*) from SESSION.tt
- result is 0 - BAD!!!

I would expect the count to still be 2.

I tried tagging on "ON ROLLBACK PRESERVE ROWS" to the table declaration but it made no difference.

thanks for any help on this.

Andy
Reply With Quote
  #2 (permalink)  
Old 09-05-08, 06:36
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Have you considered upgrading to a supported version of DB2? (V7 is out of services for nearly 3 years now.)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 09-05-08, 06:38
andrewhallam andrewhallam is offline
Registered User
 
Join Date: Dec 2003
Location: Nottingham, England
Posts: 52
Well if I can resolve this issue I might...
Reply With Quote
  #4 (permalink)  
Old 09-05-08, 06:40
andrewhallam andrewhallam is offline
Registered User
 
Join Date: Dec 2003
Location: Nottingham, England
Posts: 52
Actually its version 8.1.

My diag tool was sort of hard coded...its a long story...
Reply With Quote
  #5 (permalink)  
Old 09-05-08, 09:02
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
This does look like a bug (or an error in the manual); I would open a PMR.
Reply With Quote
  #6 (permalink)  
Old 09-05-08, 09:09
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I think I have seen this behavior also. I think it has to do with getting an error that clears the GTT. Maybe you should open a PMR with IBM.

Andy
Reply With Quote
  #7 (permalink)  
Old 09-05-08, 11:35
andrewhallam andrewhallam is offline
Registered User
 
Join Date: Dec 2003
Location: Nottingham, England
Posts: 52
Not bad - seeing as though I spotted this 'bug' in 2004!
Re-addressing it due to performance problems. (my work around is to first select from the TT using the insert values to make sure that I don't get a duplicate before actually doing the insert).

Ohh, well - if thats what you experts say then so be it.

Andy
Reply With Quote
  #8 (permalink)  
Old 09-05-08, 13:06
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
A negative return code will perform a rollback. I am not sure it is a bug. You might be able to trap the error with a condition handler.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #9 (permalink)  
Old 09-05-08, 13:51
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by andrewhallam
Not bad - seeing as though I spotted this 'bug' in 2004!
Re-addressing it due to performance problems. (my work around is to first select from the TT using the insert values to make sure that I don't get a duplicate before actually doing the insert).

Ohh, well - if thats what you experts say then so be it.

Andy
Instead of doing a select then a conditional insert, use the MERGE statement.

Andy
Reply With Quote
  #10 (permalink)  
Old 09-08-08, 03:39
andrewhallam andrewhallam is offline
Registered User
 
Join Date: Dec 2003
Location: Nottingham, England
Posts: 52
Quote:
Originally Posted by Marcus_A
A negative return code will perform a rollback. I am not sure it is a bug. You might be able to trap the error with a condition handler.
If a rollback is performed then surely adding "ON ROLLBACK PRESERVE ROWS" to the table declaration would stop the previous rows from being removed? - as the wording suggests?
Reply With Quote
  #11 (permalink)  
Old 09-08-08, 11:35
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
it will keep the rows that were comitted, but any work done between a prior commit and current rollback will be rolled back. This is not a bug it is well documented and the correct behavior.
Dave
Reply With Quote
  #12 (permalink)  
Old 09-08-08, 11:44
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
The thing is that even the previously committed rows are gone after the error. Besides, I don't believe there is an implicit rollback upon the error.
Reply With Quote
  #13 (permalink)  
Old 09-08-08, 13:42
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Besides, the clause ON ROLLBACK PRESERVE ROWS implies that no rows are deleted upon rollback either. In that, temp tables deviate from regular base tables.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #14 (permalink)  
Old 09-09-08, 04:04
andrewhallam andrewhallam is offline
Registered User
 
Join Date: Dec 2003
Location: Nottingham, England
Posts: 52
Quote:
Originally Posted by dav1mo
it will keep the rows that were comitted, but any work done between a prior commit and current rollback will be rolled back. This is not a bug it is well documented and the correct behavior.
Dave
Just tested this and you are wrong - the previous comitted rows are also deleted.

Looks like I'll have to raise a PMR with IBM.

Thanks to all who contributed to my original question.

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On