dbTalk Databases Forums  

reset a sequence in a trigger

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss reset a sequence in a trigger in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Werner Hofmann
 
Posts: n/a

Default reset a sequence in a trigger - 05-19-2005 , 08:02 AM






Hallo,

is there a solution to reset a sequence inside a trigger?

I wanted to use this procedure:

####################
CREATE OR REPLACE PROCEDURE reset_sequence (sequencename IN VARCHAR2) as
curr_val INTEGER;
BEGIN
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' MINVALUE 0';
EXECUTE IMMEDIATE 'SELECT ' ||sequencename ||'.nextval FROM dual'
INTO curr_val;
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' increment by
-'||curr_val;
EXECUTE IMMEDIATE 'SELECT ' ||sequencename ||'.nextval FROM dual' INTO
curr_val;
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' increment by 1';
END reset_sequence;
/
#####################

but the when I call the 'before insert' or 'after delete' trigger I get
the message:

--> Commit is not allowed inside a trigger.

Thank you, Werner

Reply With Quote
  #2  
Old   
ak_tiredofspam@yahoo.com
 
Posts: n/a

Default Re: reset a sequence in a trigger - 05-19-2005 , 09:23 AM






altering a sequence is a DDL statement. Oracle always precedes any DDL
with an implicit commit. And yes, commits do not belong in a trigger.

I'd try to move altering the sequence into a storee procedure with
AUTONOMOUS TRANSACTION.

Untested.


Reply With Quote
  #3  
Old   
Rauf Sarwar
 
Posts: n/a

Default Re: reset a sequence in a trigger - 05-19-2005 , 09:27 AM




Werner Hofmann wrote:
Quote:
Hallo,

is there a solution to reset a sequence inside a trigger?

I wanted to use this procedure:

####################
CREATE OR REPLACE PROCEDURE reset_sequence (sequencename IN VARCHAR2)
as
curr_val INTEGER;
BEGIN
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' MINVALUE
0';
EXECUTE IMMEDIATE 'SELECT ' ||sequencename ||'.nextval FROM dual'

INTO curr_val;
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' increment by
-'||curr_val;
EXECUTE IMMEDIATE 'SELECT ' ||sequencename ||'.nextval FROM dual'
INTO
curr_val;
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' increment by
1';
END reset_sequence;
/
#####################

but the when I call the 'before insert' or 'after delete' trigger I
get
the message:

--> Commit is not allowed inside a trigger.

Thank you, Werner
DDL's (alter ...) automatically issue a commit before execution and you
cannot commit inside a trigger... so that is why you get this error.

1) Lookup usage for pragma autonomous_transaction at
http://tahiti.oracle.com
2) What's the point of using a sequence when you are going to reset it
everytime you insert/delete a row?

Regards
/Rauf



Reply With Quote
  #4  
Old   
Werner Hofmann
 
Posts: n/a

Default Re: reset a sequence in a trigger - 05-19-2005 , 09:57 AM




Quote:
1) Lookup usage for pragma autonomous_transaction at
http://tahiti.oracle.com
2) What's the point of using a sequence when you are going to reset it
everytime you insert/delete a row?

Regards
/Rauf
Hallo Rauf,

I don't reset the sequence every time the trigger is called, but I have some
code, which is automatically cleaning the content of the table. And in the
case the cleaner has deleted all rows, then I wanted to reset the trigger.

In the firebird database I can reset the Generator(sequence) - no problem,
but oracle won't like my idea. So, if 1) is not my solution, in last
consequence I habe to reset the sequence outside the trigger anywhere in
the code.

Thank you,
Werner


Reply With Quote
  #5  
Old   
Lewis C
 
Posts: n/a

Default Re: reset a sequence in a trigger - 05-19-2005 , 05:11 PM



On Thu, 19 May 2005 15:02:18 +0200, Werner Hofmann
<superomega (AT) t-online (DOT) de> wrote:

Quote:
Hallo,

is there a solution to reset a sequence inside a trigger?

I wanted to use this procedure:

####################
CREATE OR REPLACE PROCEDURE reset_sequence (sequencename IN VARCHAR2) as
curr_val INTEGER;
BEGIN
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' MINVALUE 0';
EXECUTE IMMEDIATE 'SELECT ' ||sequencename ||'.nextval FROM dual'
INTO curr_val;
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' increment by
-'||curr_val;
EXECUTE IMMEDIATE 'SELECT ' ||sequencename ||'.nextval FROM dual' INTO
curr_val;
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' increment by 1';
END reset_sequence;
/
#####################

but the when I call the 'before insert' or 'after delete' trigger I get
the message:

--> Commit is not allowed inside a trigger.

Thank you, Werner

Stupid question maybe but why reset it? A sequence is a large number.
Are you try to save numbers? You can set a max value and then cycle
it. Resetting a sequence just seems like you're going about solving
the wrong problem.

Thanks,

Lewis




-----------------------------------------------------------
Lewis R Cunningham

Author, ItToolBox Blog: An Expert's Guide to Oracle
http://blogs.ittoolbox.com/oracle/guide/

Topic Editor, Suite101.com: Oracle Database
http://www.suite101.com/welcome.cfm/oracle

Sign up for courses here:
http://www.suite101.com/suiteu/default.cfm/416752
-----------------------------------------------------------


Reply With Quote
  #6  
Old   
Werner Hofmann
 
Posts: n/a

Default Re: reset a sequence in a trigger - 05-19-2005 , 06:35 PM



Quote:
Stupid question maybe but why reset it? A sequence is a large number.
Are you try to save numbers? You can set a max value and then cycle
it. Resetting a sequence just seems like you're going about solving
the wrong problem.

Thanks,

Lewis

In this table I have a lot of transactions, and it can be, that the ID which
is the primary key can overrun after some time (some years). I can not use
another primary key, because this would be with a VARCHAR2 column. And so I
wantet to be save, that the oldest entry have the smallest ID for sorting.

Werner


Reply With Quote
  #7  
Old   
Jim Kennedy
 
Posts: n/a

Default Re: reset a sequence in a trigger - 05-19-2005 , 09:36 PM




"Werner Hofmann" <superomega (AT) t-online (DOT) de> wrote

Quote:
Stupid question maybe but why reset it? A sequence is a large number.
Are you try to save numbers? You can set a max value and then cycle
it. Resetting a sequence just seems like you're going about solving
the wrong problem.

Thanks,

Lewis


In this table I have a lot of transactions, and it can be, that the ID
which
is the primary key can overrun after some time (some years). I can not use
another primary key, because this would be with a VARCHAR2 column. And so
I
wantet to be save, that the oldest entry have the smallest ID for sorting.

Werner
Look at global temporary tables. This will probably do what you want very
nicely and not reset the sequence. I could see some major concurrency
issues with resetting the sequence dynamically like that.
Jim




Reply With Quote
  #8  
Old   
Akp
 
Posts: n/a

Default Re: reset a sequence in a trigger - 05-19-2005 , 11:07 PM



Hi Werner,

Try using the below trigger . This will not give the Error . DDL
Commands have implicit commit attached to it . Use of Autonomous
Transaction feature will eliminate the error message. True in 8i and
above .

####################
CREATE OR REPLACE PROCEDURE reset_sequence (sequencename IN VARCHAR2)
as
curr_val INTEGER;
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' MINVALUE 0';

EXECUTE IMMEDIATE 'SELECT ' ||sequencename ||'.nextval FROM
dual'
INTO curr_val;
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' increment
by
-'||curr_val;
EXECUTE IMMEDIATE 'SELECT ' ||sequencename ||'.nextval FROM
dual' INTO
curr_val;
EXECUTE IMMEDIATE 'alter sequence ' ||sequencename||' increment
by 1';
END reset_sequence;
/
#####################

Cheers
Abhishek


Reply With Quote
  #9  
Old   
Werner Hofmann
 
Posts: n/a

Default Re: reset a sequence in a trigger - 05-20-2005 , 12:44 AM




Quote:
1) Lookup usage for pragma autonomous_transaction at
http://tahiti.oracle.com
2) What's the point of using a sequence when you are going to reset it
everytime you insert/delete a row?

Regards
/Rauf

Hallo Rauf,

PRAGMA AUTONOMOUS_TRANSACTION;
is working fine.

Thank you,
Werner


Reply With Quote
  #10  
Old   
Ed Prochak
 
Posts: n/a

Default Re: reset a sequence in a trigger - 05-20-2005 , 12:45 AM



Werner Hofmann wrote:

Quote:
1) Lookup usage for pragma autonomous_transaction at
http://tahiti.oracle.com
2) What's the point of using a sequence when you are going to reset it
everytime you insert/delete a row?

Regards
/Rauf


Hallo Rauf,

I don't reset the sequence every time the trigger is called, but I have some
code, which is automatically cleaning the content of the table. And in the
case the cleaner has deleted all rows, then I wanted to reset the trigger.

In the firebird database I can reset the Generator(sequence) - no problem,
but oracle won't like my idea. So, if 1) is not my solution, in last
consequence I habe to reset the sequence outside the trigger anywhere in
the code.

Thank you,
Werner
You don't reset the trigger, you reset the sequence.

Why can't you do it in your code that does the automatic cleaning??

Ed

--
Ed Prochak
running http://www.faqs.org/faqs/running-faq/
netiquette http://www.psg.com/emily.html
--
"Two roads diverged in a wood and I
I took the one less travelled by
and that has made all the difference."
robert frost



Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.