如何在Microsoft SQL Server中实现序列? [英] How would you implement sequences in Microsoft SQL Server?
问题描述
有没有人有一个好的方法来实现类似SQL Server中的序列?
Does anyone have a good way of implementing something like a sequence in SQL server?
有时你只是不想使用GUID,他们是丑陋的heck。也许你想要的序列不是数字?
Sometimes you just don't want to use a GUID, besides the fact that they are ugly as heck. Maybe the sequence you want isn't numeric? Besides, inserting a row and then asking the DB what the number is just seems so hackish.
推荐答案
Sql Server 2012已经引入了< a href =http://msdn.microsoft.com/en-us/library/ff878058.aspx =noreferrer> SEQUENCE
对象,
Sql Server 2012 has introduced SEQUENCE
objects, which allow you to generate sequential numeric values not associated with any table.
创建它们很容易:
CREATE SEQUENCE Schema.SequenceName
AS int
INCREMENT BY 1 ;
插入前使用它们的示例:
An example of using them before insertion:
DECLARE @NextID int ;
SET @NextID = NEXT VALUE FOR Schema.SequenceName;
-- Some work happens
INSERT Schema.Orders (OrderID, Name, Qty)
VALUES (@NextID, 'Rim', 2) ;
查看我的博客,深入了解如何使用序列:
See my blog for an in-depth look at how to use sequences:
http ://sqljunkieshare.com/2011/12/11/sequences-in-sql-server-2012-implementingmanaging-performance/
这篇关于如何在Microsoft SQL Server中实现序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!