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 > Rollback of a sequence value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-30-11, 11:24
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
Rollback of a sequence value

Hi,

Is there any way to rollback a sequence value in a procedure? I'm trying to run this code and it keeps incrementing the value of the sequence seq_id..

CREATE PROCEDURE NRG1.TEST (OUT msg VARCHAR(128))
RESULT SETS 0
MODIFIES SQL DATA
NOT DETERMINISTIC
LANGUAGE SQL
BEGIN

DECLARE id INTEGER DEFAULT -1;

VALUES nextval FOR seq_id INTO id;
IF id > 0 THEN
ROLLBACK;
SET msg = 'Rolling back value of sequence ';
RETURN;
END IF;

END
@



If I give -- db2 "VALUES prevval FOR seq_id" it keeps showing me the next value in the sequence so basically it is incrementing it even though I put 'rollback' in the procedure. Is there any way to achieve this? Thanks!!
Reply With Quote
  #2 (permalink)  
Old 06-30-11, 11:35
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by db2user24 View Post
If I give -- db2 "VALUES prevval FOR seq_id" it keeps showing me the next value in the sequence so basically it is incrementing it even though I put 'rollback' in the procedure. Is there any way to achieve this? Thanks!!
No, that's not possible. Sequences are non-transactional. That's actually their big advantage otherwise they would not scale well for a large number of concurrent sessions.
Reply With Quote
  #3 (permalink)  
Old 06-30-11, 11:58
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
thank you.. that helps!
Reply With Quote
  #4 (permalink)  
Old 06-30-11, 15:25
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The only option would be to reset/restart the sequence using the ALTER SEQUENCE statement. But then you would loose all guarantees that the sequence values are unique.

p.s: If you want to rely on sequences to have no gaps in the data being produced, sequences are the wrong thing anyway. Due to the internal caching, it may happen that sequence values are available for a transaction but never used by it. Those values will not be used by other transactions either.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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