Beruflich Dokumente
Kultur Dokumente
Syntax
CREATE SEQUENCE [schema_name . ] sequence_name
[ AS [ built_in_integer_type | user-defined_integer_type ] ]
[ START WITH <constant> ]
[ INCREMENT BY <constant> ]
[ { MINVALUE [ <constant> ] } | { NO MINVALUE } ]
[ { MAXVALUE [ <constant> ] } | { NO MAXVALUE } ]
[ CYCLE | { NO CYCLE } ]
[ { CACHE [ <constant> ] } | { NO CACHE } ]
[ ; ]
Arguments
sequence_name
Specifies the unique name by which the sequence is known in the database. Type
is sysname.
[ built_in_integer_type | user-defined_integer_type
A sequence can be defined as any integer type. The following types are allowed.
tinyint - Range 0 to 255
smallint - Range -32,768 to 32,767
int - Range -2,147,483,648 to 2,147,483,647
bigint - Range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
decimal and numeric with a scale of 0.
Any user-defined data type (alias type) that is based on one of the allowed
types.
If no data type is provided, the bigint data type is used as the default.
START WITH <constant>
The first value returned by the sequence object. The START value must be a value
less than or equal to the maximum and greater than or equal to the minimum value
of the sequence object. The default start value for a new sequence object is the
minimum value for an ascending sequence object and the maximum value for a
descending sequence object.
INCREMENT BY <constant>
Value used to increment (or decrement if negative) the value of the sequence object
for each call to the NEXT VALUE FOR function. If the increment is a negative value,
the sequence object is descending; otherwise, it is ascending. The increment cannot
be 0. The default increment for a new sequence object is 1.
[ MINVALUE <constant> | NO MINVALUE ]
Specifies the bounds for the sequence object. The default minimum value for a new
sequence object is the minimum value of the data type of the sequence object. This
is zero for the tinyint data type and a negative number for all other data types.
[ MAXVALUE <constant> | NO MAXVALUE
Specifies the bounds for the sequence object. The default maximum value for a new
sequence object is the maximum value of the data type of the sequence object.
[ CYCLE | NO CYCLE ]
Property that specifies whether the sequence object should restart from the minimum
value (or maximum for descending sequence objects) or throw an exception when its
minimum or maximum value is exceeded. The default cycle option for new sequence
objects is NO CYCLE.
Note that cycling restarts from the minimum or maximum value, not from the start
value.
[ CACHE [<constant> ] | NO CACHE ]
Increases performance for applications that use sequence objects by minimizing the
number of disk IOs that are required to generate sequence numbers.
For example, if a cache size of 50 is chosen, SQL Server does not keep 50 individual
values cached. It only caches the current value and the number of values left in the
cache. This means that the amount of memory required to store the cache is always
two instances of the data type of the sequence object.
Note
If the cache option is enabled without specifying a cache size, the Database Engine will select a size.
However, users should not rely upon the selection being consistent. Microsoft might change the
method of calculating the cache size without notice.
When created with the CACHE option, an unexpected shutdown (such as a power
failure) may result in the loss of sequence numbers remaining in the cache.
General Remarks
Sequence numbers are generated outside the scope of the current transaction. They are
consumed whether the transaction using the sequence number is committed or rolled back.
Cache management
The following process occurs every time that a sequence object is requested to generate the
next value for the NO CACHEoption:
1. The next value for the sequence object is calculated.
2. The new current value for the sequence object is written to the system table.
3. The calculated value is returned to the calling statement.
Metadata
Security
Permissions
Requires CREATE SEQUENCE, ALTER, or CONTROL permission on the SCHEMA.
Members of the db_owner and db_ddladmin fixed database roles can create, alter,
and drop sequence objects.
Members of the db_owner and db_datawriter fixed database roles can update
sequence objects by causing them to generate numbers.
The following example grants the user AdventureWorks\Larry permission to create
sequences in the Test schema.
GRANT CREATE SEQUENCE ON SCHEMA::Test TO [AdventureWorks\Larry]
Ownership of a sequence object can be transferred by using the ALTER
AUTHORIZATION statement.
If a sequence uses a user-defined data type, the creator of the sequence must have
REFERENCES permission on the type.
Audit
To audit CREATE SEQUENCE, monitor the SCHEMA_OBJECT_CHANGE_GROUP.
Examples
For examples of creating sequences and using the NEXT VALUE FOR function to generate
sequence numbers, see Sequence Numbers.
Most of the following examples create sequence objects in a schema named Test.
To create the Test schema, execute the following statement.
-- CREATE SCHEMA Test ;
GO
INCREMENT BY -1 ;
GO
start_value
-9223372036854775808
increment
mimimum_value
-9223372036854775808
maximum_value
9223372036854775807
is_cycling
is_cached
current_value
-9223372036854775808
The following example creates a sequence named DecSeq using the decimal data type,
having a range from 0 to 255. The sequence starts with 125 and increments by 25 every
time that a number is generated. Because the sequence is configured to cycle when the
value exceeds the maximum value of 200, the sequence restarts at the minimum value of
100.
CREATE SEQUENCE Test.DecSeq
AS decimal(3,0)
START WITH 125
INCREMENT BY 25
MINVALUE 100
MAXVALUE 200
CYCLE
CACHE 3
;
Execute the following statement to see the first value; the START WITH option of 125.
SELECT NEXT VALUE FOR Test.DecSeq;
Execute the statement three more times to return 150, 175, and 200.
Execute the statement again to see how the start value cycles back to the MINVALUE option
of 100.
Execute the following code to confirm the cache size and see the current value.
SELECT cache_size, current_value
FROM sys.sequences
WHERE name = 'DecSeq' ;
See Also
Reference
ALTER SEQUENCE (Transact-SQL)
DROP SEQUENCE (Transact-SQL)
NEXT VALUE FOR (Transact-SQL)
Concepts
Sequence Numbers