[AS datatype]
[START WITH value]
[INCREMENT BY value]
[MINVALUE value | NO MINVALUE]
[MAXVALUE value | NO MAXVALUE]
[CYCLE | NO CYCLE]
[CACHE value | NO CACHE];
Parameters:
CREATE SEQUENCE contacts_seq
AS BIGINT
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999
NO CYCLE
CACHE 10;
Here we have created a sequence named contacts_seq , starting from value 1, each of the following values increases by one unit (ie 2, 3, 4 .). The string will store about 10 values into the cache. The maximum value in the sequence is 99999 and it will not restart the string after the sequence ends at the largest value.
You can also execute the above command simply as follows:
CREATE SEQUENCE contacts_seq
START WITH 1
INCREMENT BY 1;
So now you've created a sequence that simulates an autonumber field. Next, to output a value from this sequence we use the NEXT VALUE FOR command
SELECT NEXT VALUE FOR contacts_seq;
This statement will retrieve the next value from contacts_seq. Then use the necessary commands for your execution. For example:
INSERT INTO contacts
(contact_id, last_name)
VALUES
(NEXT VALUE FOR contacts_seq, 'Smith');
This INSERT statement inserts a new record into the contact table. The contact_id field will be assigned the next number from contacts_seq, the last_name field will be 'Smith'.
Once a sequence has been created, there will also be cases where you want to remove the sequence from the database for several reasons.
To delete a sequence, we have the following syntax:
DROP SEQUENCE sequence_name;
Parameters:
sequence_name: The sequence name you want to delete.
DROP SEQUENCE contacts_seq;
By executing this command, you have just removed the contacts_seq string from the database.
To test sequence properties, we have the following syntax:
SELECT * FROM sys.sequences WHERE name = ' sequence_name' ;
Parameters:
sequence_name: The name of the string to test the property.
SELECT *
FROM sys.sequences
WHERE name = 'contacts_seq';
This example queries information from the sys.sequences system and retrieves the result for the contacts_seq string .
Sys.sequences system includes the following columns:
COLLECTION NOTE name of the sequence created in the statement CREATE SEQUENCE object_id ID of the principal_id object of the sequence ID principal (numeric value) schema_id Schema ID of sequence parent_object_id ID of the parent object type SO type_desc SEQUENCE_OBJECT create_date Date / Time created sequence with the CREATE SEQUENCE command modify_date Date / Time of last modification sequence is_ms_shipped Value 0 or 1 is_published Value 0 or 1 is_schema_published Value 0 or 1 start_value Start value of the sequence increment Rule value of sequence increase / decrease minimum_value Minimum value in string maximum_value Maximum value in is_cycling string Value 0 or 1. 0 = NO CYCLE, 1 = CYCLE is_cached Value 0 or 1, 0 = NO CACHE, 1 = CACHE cache_size Memory size buffer when is_cached = 1 system_type_id ID system of sequence user_type_id ID worry at sequence precision user Maximum accuracy for data type of maximum range scale for the sequence of sequence current_value Last value retrieved from sequence is_exhausted Value 0 or 1. 0 = Multiple values in sequence. 1 = No value
Previous article: Declare variables in SQL Server
Next lesson: FUNCTION (Function) in SQL Server