|
|
|
Variable |
Description |
|
INCREMENT BY |
The increment value. This can be a positive or negative number. |
|
START WITH |
The start value for the sequence. |
|
MAXVALUE |
The maximum value that the sequence can generate. If specifying NOMAXVALUE, the maximum value is 263-1. |
|
MINVALUE |
The minimum value that the sequence can generate. If specifying NOMINVALUE, the minimum value is -263. |
|
CYCLE |
Specify CYCLE to indicate that when the maximum value is reached the sequence starts over again at the start value. Specify NOCYCLE to generate an error upon reaching the maximum value. |
To drop a sequence, execute a DROP SEQUENCE statement. Use this function when a sequence is no longer useful, or to reset a sequence to an older number. To reset a sequence, first drop the sequence and then recreate it.
Drop a sequence following this format:
DROP SEQUENCE my_sequence
Use sequences when an application requires a unique identifier. INSERT statements, and occasionally UPDATE statements, are the most common places to use sequences. Two "functions" are available on sequences:
NEXTVAL: Returns the next value from the sequence.
CURVAL: Returns the value from the last call to NEXTVAL by the current user during the current connection. For example, if User A calls NEXTVAL and it returns 124, and User B immediately calls NEXTVAL getting 125, User A will get 124 when calling CURVAL, while User B will get 125 while calling CURVAL. It is important to understand the connection between the sequence value and a particular connection to the database. The user cannot call CURVAL until making a call to NEXTVAL at least once on the connection. CURVAL returns the current value returned from the sequence on the current connection, not the current value of the sequence.
To create the sequence:
CREATE SEQUENCE customer_seq INCREMENT BY 1 START WITH 100
To use the sequence to enter a record into the database:
INSERT INTO customer (cust_num, name, address)
VALUES (customer_seq.NEXTVAL, 'John Doe', '123 Main St.')
To find the value just entered into the database:
SELECT customer_seq.CURVAL AS LAST_CUST_NUM
Copyright BASIS International Ltd. BBj®, Visual PRO/5®, PRO/5®, and BBx® are registered trademarks.