# ALTER SEQUENCE (Transact-SQL)

## Syntax

``````ALTER SEQUENCE [schema_name. ] sequence_name
[ RESTART [ WITH <constant> ] ]
[ INCREMENT BY <constant> ]
[ { MINVALUE <constant> } | { NO MINVALUE } ]
[ { MAXVALUE <constant> } | { NO MAXVALUE } ]
[ CYCLE | { NO CYCLE } ]
[ { CACHE [ <constant> ] } | { NO CACHE } ]
[ ; ]
``````

## 引數

sequence_name

RESTART [ WITH < constant > ]

INCREMENT BY < 常數>

[ MINVALUE < 常數 > |NO MINVALUE ]

[ MAXVALUE < 常數 > |NO MAXVALUE

[ CYCLE | NO CYCLE ]

[ CACHE [ < constant > ] |NO CACHE ]

## 安全性

### 權限

``````GRANT ALTER ON OBJECT::Test.TinySeq TO [AdventureWorks\Larry]
``````

## 範例

### A. 改變順序

``````CREATE SCHEMA Test ;
GO

CREATE SEQUENCE Test.TestSeq
AS int
INCREMENT BY 25
MINVALUE 100
MAXVALUE 200
CYCLE
CACHE 3
;
GO
``````

``````ALTER SEQUENCE Test. TestSeq
INCREMENT BY 50
MINVALUE 50
MAXVALUE 200
NO CYCLE
NO CACHE
;
GO
``````

### B. 重新啟動順序

``````CREATE SEQUENCE Test.CountBy1 ;
``````

``````SELECT NEXT VALUE FOR Test.CountBy1
``````

``````ALTER SEQUENCE Test.CountBy1 RESTART WITH 1 ;
``````

``````SELECT NEXT VALUE FOR Test.CountBy1;
``````

CountBy1 順序是使用 NO CYCLE 預設值建立的，所以它在產生 9,223,372,036,854,775,807 數字之後會停止運作。 順序物件的後續呼叫會傳回錯誤 11728。 下列陳述式會將順序物件變更為循環，並設定 20 的快取。

``````ALTER SEQUENCE Test.CountBy1
CYCLE
CACHE 20 ;
``````