使用過Oracle的都知道,Oracle中的自增是靠序列來完成的,在必定程度上蠻方便的。如今SQL 2012中也有序列了。來看看怎麼作的吧!ui
SQL Server 如今將序列當成一個對象來實現,建立一個序列的語法以下:spa
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 } ] [ ; ]
建立示例:3d
CREATE SEQUENCE Sequence_Test AS [bigint] START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 9223372036854775807 CACHE GO
使用序列:code
SELECT NEXT VALUE FOR Sequence_Test 或者下面查詢序列屬性 SELECT * FROM sys.sequences WHERE name = 'Sequence_Test';
重置序列:對象
ALTER SEQUENCE Sequence_Test
RESTART WITH 1 ;
序列可定義爲任意整數類型,以下面類型:blog
tinyint - 範圍從 0 到 255 ci
smallint - 範圍從 -32,768 到 32,767 input
int - 範圍從 -2,147,483,648 到 2,147,483,647 it
bigint - 範圍從 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 class
decimal 和 numeric,小數位數爲 0。