Oracle sql format max number 99999999999 - sql

Is the number 9999999999999999999999999999 can be written better?
I have multiple queries with that value.
CREATE SEQUENCE "INVOICES_SEQ" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 21 CACHE 20 NOORDER NOCYCLE ;

I don't believe you need to write out the maxvalue. Use NOMAXVALUE, which caps at the 28-digit number.
You could also replace MINVALUE 1 with NOMINVALUE.
ORACLE DOCS:
NOMAXVALUE Specify NOMAXVALUE to indicate a maximum value of 10 27th power
for an ascending sequence or -1 for a descending sequence. This is the
default.
NOMINVALUE Specify NOMINVALUE to indicate a minimum value of 1 for
an ascending sequence or -10 26th power for a descending sequence. This is the
default.
CREATE SEQUENCE "invoices_seq" NOMINVALUE NOMAXVALUE INCREMENT BY 1 START WITH 21 CACHE 20 NOORDER NOCYCLE;

It is good practice to specify only non-default settings.
All of those values except the starting value are the defaults and therefore don't need to be stated (and in my view shouldn't be), so the same sequence can be created more concisely using
create sequence invoices_seq start with 21;
For the more general case in your question title, 9999999999999999999999999999 is 1e28 - 1.

Related

Sequence id is not properly inserted

I have created a sequence but its not inserting ids in sequence order.
For Ex:
First I have created one set of record seq number generated as 1, 2, 3, 4
Again I have created another set of records seq started from 8, 9, 10
For 3rd time I have created another set of records seq id got generated as 5, 6, 7
(which is not correct, I want the seq id to be continued as 11, 12, 13)
So 5, 6, 7 is wrong, I need 11, 12, 13 to be generated
What's wrong in my below sequence create query?
CREATE SEQUENCE "LEASE_REPAYMENT_SEQ"
MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1
START WITH 146724 CACHE 20 NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL ;
If you check the sequence definition, you'll see that you define it as NOORDER.
The Oracle Documentation says
Specify NOORDER if you do not want to guarantee sequence numbers are generated in order of request. This is the default.
So what you see is an expected bahaviour and I assume your database is a RAC instance (as this effect can be observed on RAC only).
Having said that, there are good reasons to allow this small dis-order of the assigned IDs (which is caused by the caching as each RAC instance gets it own cache size to work with).
The positive side of this apprach is that there is no need to synchronize the sequence between the instances - a task that could produce a big overhead.

How to make sequence jump from 100 to 150

How to fire sequence to jump from 100 to 150 and followed by 152,153 and so on . .
If your sequence seq1 is now at 100 and you want it to go to 150 you can:
Change its increment to 50.
Use it once.
Change its increment back to 1.
In SQL terms:
alter sequence seq1 increment by 50;
select seq1.nextval from dual;
alter sequence seq1 increment by 1;

What is the purpose of the "START WITH" part of a SQL sequence?

In oracle SQL, a sequence is dfined like so :
CREATE SEQUENCE sup_seq
MINVALUE 1
MAXVALUE 999
START WITH 5
INCREMENT BY 1
CACHE 20;
What is the point of having the "START WITH" part ? If we have a minimum value , doesn't that imply that we start with that value? What is useful regarding START WITH ?
Normally, it would be pretty unusual to specify both MINVALUE and START WITH when defining a sequence and to have those values be different. Personally, I don't think that I've ever explicitly specified a MINVALUE since I find START WITH to be more self-explanatory.
One case where two values would be useful would be a sequence that is set to cycle. For example
CREATE SEQUENCE sup_seq
MINVALUE 1
MAXVALUE 999
START WITH 5
INCREMENT BY 1
CACHE 20
CYCLE;
would start with 5 (the start with), generate values through 999 (the maxvalue), and then restart at 1 (the minvalue). That's not a particularly common use case but it's entirely possible.

Oracle Sequence generator syntax

The basic syntax for creating a sequence generator for oracle database table attribute is:
CREATE SEQUENCE customers_seq
START WITH 1000
INCREMENT BY 1
NOCACHE
NOCYCLE;
I would like to know what does the NOORDER clause in the SEQUENCE syntax do? If I include the NOORDER clause, will it give me a sequence keeping the increment value random???
This is important when running in parallel server mode or in a cluster. ORDER guarantees that the sequences numbers are ordered.
Example of “noorder” sequence in 10g RAC:
Session 1 on node-A: nextval -> 101
Session 2 on node-A: nextval -> 102
Session 1 on node-B: nextval -> 121
Session 1 on node-B: nextval -> 122
Session 1 on node-A: nextval -> 103
Session 1 on node-A: nextval -> 104
NOORDER in combination with CACHING sequences results in this behavior. ORDER effectively makes the CACHE setting unused. With caching set to 10 when a session gets a sequences it takes 10 sequences into it's cache.

How to increase non-numeric column value?

I have a column that is used for a sequence number.
The values in the column look like 'WT0000004568'
I need to find the maximum value and increment the counter part by 1 to create a new sequence number.
For this example, the resultant value would be 'WT0000004569'.
How do I do that?
As Thilio pointed out, this is generally a bad design both from a performance and from a concurrency issue. If we assume that you have a single user system and aren't particularly concerned about performance, you could do something like
SQL> ed
Wrote file afiedt.buf
1 select 'WT' ||
2 to_char(
3 to_number(substr('WT0000004568',3)) + 1,
4 'fm0000000000')
5* from dual
SQL> /
'WT'||TO_CHAR
-------------
WT0000004569