Sunteți pe pagina 1din 6

Skip to content

Skip to menu
Exploring Oracle: No Stone Unturned
Search
Home
About
May 11 2011
Category: Database,Sequence ittichai @ 7:51 pm
This is not new but Ive learned about it last week from one of developers when reviewing codes with them for the
plan to change the sequences current value. Normally I would drop the sequence and create it with START WITH to a
new desired number. However, my approach will obviously invalidate all dependencies which commonly refer to
triggers.
The trick is to change the INCREMENT BY value to the difference between the current value and the needed value, then
use it once, finally switch the INCREMENT BY back to 1.
Here is a quick demo:
This TR_TAB_A_BI trigger depends on the SQ_TAB_A sequence.
create or replace trigger TR_TAB_A_BI
before insert on TAB_A
for each row
when (new.id is null)
begin
select SQ_TAB_A.nextval
into :new.id
from dual;
end;
/
Start with trigger valid.
SQL> select object_name, status from user_objects where object_name='TR_TAB_A_BI';
OBJECT_NAME STATUS
-------------------- -------
TR_TAB_A_BI VALID
Lets drop trigger.
SQL> drop sequence SQ_TAB_A;
Change sequences current value (CURRVAL) without dropping it Or... http://oraexplorer.com/2011/05/change-sequences-current-value-currval-...
1 of 6 24-Sep-14 1:16 AM
Sequence dropped.
As expected, the trigger became invalid.
SQL> select object_name, status from user_objects where object_name='TR_TAB_A_BI';
OBJECT_NAME STATUS
-------------------- -------
TR_TAB_A_BI INVALID
After the sequence is recreated, you can compile this trigger before use, or you can just execute an insert into the table,
Oracle will recompile this trigger automatically before actual use.
SQL> create sequence SQ_TAB_A start with 243 cache 20;
Sequence created.
SQL> select object_name, status from user_objects where object_name='TR_TAB_A_BI';
OBJECT_NAME STATUS
-------------------- -------
TR_TAB_A_BI INVALID
SQL> insert into TAB_A (B) values ('AA');
1 row created.
SQL> select object_name, status from user_objects where object_name='TR_TAB_A_BI';
OBJECT_NAME STATUS
-------------------- -------
TR_TAB_A_BI VALID
Neat!
The other way to avoid triggers being invalidated at all, as mentioned in the first paragraph, is to change the
INCREMENT BY value.
SQL> select object_name, status from user_objects where object_name='TR_TAB_A_BI';
OBJECT_NAME STATUS
-------------------- -------
TR_TAB_A_BI VALID
Assuming the current value is 300, we want a new current value to be 500. So the difference is 200.
SQL> alter sequence SQ_TAB_A increment by 200 nocache;
Sequence altered.
SQL> select SQ_TAB_A.nextval from dual;
NEXTVAL
----------
500
SQL> alter sequence SQ_TAB_A increment by 1 cache 20;
Sequence altered.
With all changes in sequence, the trigger will remain valid.
SQL> select object_name, status from user_objects where object_name='TR_TAB_A_BI';
OBJECT_NAME STATUS
-------------------- -------
Change sequences current value (CURRVAL) without dropping it Or... http://oraexplorer.com/2011/05/change-sequences-current-value-currval-...
2 of 6 24-Sep-14 1:16 AM
TR_TAB_A_BI VALID
Tags: current value, CURRVAL, drop, dropping, next value, NEXTVAL, sequence
Comments (3)
3 Responses to Change sequences current value (CURRVAL) without dropping it
Gino Law says:
May 31st, 2011 3:37 pm
I gotta bookmark this site it seems handy handy.
Reply
1.
Monir says:
February 5th, 2013 10:26 am
Very helpfull. thanks
Reply
2.
tyler says:
April 30th, 2013 1:40 pm
Thats a useful tip! I usually write a loop to increment the sequence. That wouldnt work if the sequence is to be
decremented, but then you dont want that most of the time!
SQL>
SQL> select my_seq.currval from dual;
CURRVAL
-
21
1 row selected.
SQL>
SQL> Lets say I want to increment this sequence value till 30
SQL>
SQL> var n number;
SQL>
SQL> exec for i in 1..(30 21) loop :n := my_seq.nextval; end loop;
PL/SQL procedure successfully completed.
SQL>
3.
You may also like -
Sponsored Content by nRelate
A new extended partition syntax in 11g
Oracle 10.2.0.3 Gap Resolution of Physical Standby ...
Oracle 11g SQL Error Logging
What Does 24/7 Support Mean? eSecureData - Sponsored
Change sequences current value (CURRVAL) without dropping it Or... http://oraexplorer.com/2011/05/change-sequences-current-value-currval-...
3 of 6 24-Sep-14 1:16 AM
Privacy & Terms
SQL> select my_seq.currval from dual;
CURRVAL
-
30
1 row selected.
SQL>
SQL>
Reply
Leave a Reply
Name (required)
Mail (will not be published) (required)
Website
Comment text
Notify me of followup comments via e-mail

Notify me of new posts by email.
Recent Comments
search engine on Book Review: Oracle SQL Developer 2.1 by Sue Harper
rekha on SQL and PL/SQL reserved words
ittichai on File Browser in APEX 4 with BLOB column specified in item source attribute
ittichai on Read XML data from URL and insert into Oracle
Dushan on Read XML data from URL and insert into Oracle
Favorite Technical Links
Change sequences current value (CURRVAL) without dropping it Or... http://oraexplorer.com/2011/05/change-sequences-current-value-currval-...
4 of 6 24-Sep-14 1:16 AM
Ardent Performance Computing
Chicago Oracle Users Group
Coskans Approach to Oracle
Dan Norriss Blog
daust_de :: Oracle XE / Apex
Denes Kubicek ApEx BLOG
Eddie Awads Blog
Inside Oracle APEX
Johns Blog
Kevin Clossons Oracle Blog
Laurent Schneider
Oracle APEX
Oracle FAQs
Oracle News Aggregator
Oracle Scratchpad
OraStory
The Arup Nanda Blog
The Oracle Alchemist
The ORACLE-BASE Blog
The Tom Kyte Blog
Tyler Muths Blog
Vit Spinka Technical Blog
View my page on Oracle Community
Change sequences current value (CURRVAL) without dropping it Or... http://oraexplorer.com/2011/05/change-sequences-current-value-currval-...
5 of 6 24-Sep-14 1:16 AM
Change sequences current value (CURRVAL) without dropping it Or... http://oraexplorer.com/2011/05/change-sequences-current-value-currval-...
6 of 6 24-Sep-14 1:16 AM

S-ar putea să vă placă și