Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of sequence in SQL Server 2012

I have SQL Server 2012 and I want to know what's the usage of sequence. I Look for a sample to explain usage of sequence.

EDIT

I know create and use Sequence in database. I want to know what is practical scenario for use of Sequence.

like image 370
mehdi lotfi Avatar asked Nov 04 '12 05:11

mehdi lotfi


People also ask

What is the use of sequence in SQL Server?

A sequence is a user-defined schema bound object that generates a sequence of numeric values according to the specification with which the sequence was created. The sequence of numeric values is generated in an ascending or descending order at a defined interval and can be configured to restart (cycle) when exhausted.

What is the use of sequence in database?

A sequence is a database object that allows the automatic generation of values, such as check numbers. Sequences are ideally suited to the task of generating unique key values. Applications can use sequences to avoid possible concurrency and performance problems resulting from column values used to track numbers.

Can we use sequence in SQL Server?

A sequence is a list of numbers, in an ordered manner. For example, {1, 2, 3} is a sequence and {3, 2, 1} is also sequence but a different sequence. It is a user-defined schema object that produces a list of numbers in accordance to specified value in SQL server.


1 Answers

CREATE SEQUENCE dbo.OrderIDs
    AS INT
    MINVALUE 1
    NO MAXVALUE
    START WITH 1;

SELECT NextOrderID = NEXT VALUE FOR dbo.OrderIDs
UNION ALL     SELECT NEXT VALUE FOR dbo.OrderIDs
UNION ALL     SELECT NEXT VALUE FOR dbo.OrderIDs;

Results:

NextOrderID
-----------
1
2
3

See here for original source and more examples. The page refers to SQL Server Denali which is the beta of SQL 2012 but the syntax is still the same.

like image 73
Keith Avatar answered Oct 17 '22 11:10

Keith