How would you implement sequences in Microsoft SQL Server?(您将如何在 Microsoft SQL Server 中实现序列?)
问题描述
有没有人有在 SQL Server 中实现类似序列的好方法?
Does anyone have a good way of implementing something like a sequence in SQL server?
有时你只是不想使用 GUID,除了它们很丑的事实.也许您想要的序列不是数字?此外,插入一行然后询问数据库数字是什么似乎太hackish了.
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 引入了 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 中实现序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:您将如何在 Microsoft SQL Server 中实现序列?
基础教程推荐
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
