Reprint Link: http://www.cnblogs.com/zhangyoushugz/archive/2012/11/09/2762720.html
As we all know, in the previous version of SQL Server, the GUID or identity is generally used as the identifier, but the identity is a table object, can only guarantee the sequence in a table, when we encounter the following situations,
Table 1
Id |
Order type |
Price |
1 |
Train tickets |
200 |
4 |
Plane ticket |
2000 |
5 |
Tickets |
600 |
Table 2
Id |
Order type |
Price |
2 |
Hotel |
400 |
3 |
Park tickets |
170 |
6 |
Rental fee |
300 |
For example, we need to implement ID consistency between multiple tables, and in SQL Server there will be some trouble, usually we will use an extra temp table to map the relationship of these IDs and then take the sequence from it.
Sequence is very early in Oracle, and SQL Server 2012 's sequence function is similar to that of a schema-based object, so it can be called by multiple tables.
The sequence syntax is as follows:
- 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}]
- [ ; ]
CREATE SEQUENCE [schema_name.] Sequence_name [as [Built_in_integer_type | user-defined_integer_type]] [STAR T with <constant>] [INCREMENT by <constant>] [{MINVALUE [<constant>]} | {NO MINVALUE}] [{MAXVALUE [<constant>]} | {NO MAXVALUE}] [CYCLE | {NO CYCLE}] [{CACHE [<constant>]} | {NO CACHE}] [ ; ]
Start with Set start value
Increment by setting the growth interval
Min and Max set minimum and maximum values respectively
Cycle setting is loop
cache specifies settings for caching
SQL Server 2012 developing a new feature sequence object (Sequence) (GO)