46

我想从我的序列中获取当前值 - 与序列属性窗口 SQL Server Management Studio 中显示的值相同

在此处输入图像描述

我的序列是用这个语句创建的:

CREATE SEQUENCE [OrderNumberSequence]
    as int
    START WITH 4000
    INCREMENT BY 1
    MINVALUE 0
    NO MAXVALUE
    NO CACHE;
GO

从 MSDN尝试过这个 SQL——但结果是每次运行查询时我的数字都会增加 5

DECLARE  
  @FirstSeqNum sql_variant
, @LastSeqNum sql_variant
, @CycleCount int
, @SeqIncr sql_variant
, @SeqMinVal sql_variant
, @SeqMaxVal sql_variant ;

EXEC sys.sp_sequence_get_range
@sequence_name = N'[OrderNumberSequence]'
, @range_size = 5
, @range_first_value = @FirstSeqNum OUTPUT 
, @range_last_value = @LastSeqNum OUTPUT 
, @range_cycle_count = @CycleCount OUTPUT
, @sequence_increment = @SeqIncr OUTPUT
, @sequence_min_value = @SeqMinVal OUTPUT
, @sequence_max_value = @SeqMaxVal OUTPUT ;

-- The following statement returns the output values
SELECT
  @FirstSeqNum AS FirstVal
, @LastSeqNum AS LastVal
, @CycleCount AS CycleCount
, @SeqIncr AS SeqIncrement
, @SeqMinVal AS MinSeq
, @SeqMaxVal AS MaxSeq ;

有没有办法在不改变数字的情况下获得价值?

4

1 回答 1

83

您可以选择 current_valuesys.sequences

SELECT current_value FROM sys.sequences WHERE name = 'OrderNumberSequence' ;

演示

于 2012-12-04T12:12:18.900 回答