如何从SQLServer序列中获取当前值?

  • 回答 (1)
  • 关注 (0)
  • 查看 (78)

我想从我的序列中获得当前值--与序列属性窗口SQLServerManagementStudio中显示的值相同

我的序列是用以下语句创建的:

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 ;

有什么方法可以不改变数字就得到值吗?

uncle_lightuncle_light提问于
麦大师程序猿回答于
已采纳
SELECT current_value FROM sys.sequences WHERE name = 'OrderNumberSequence' ;

扫码关注云+社区

领取腾讯云代金券