替代选择 <sequence>.nextval from dual of Oracle in SQL Server?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8969504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternate for select <sequence>.nextval from dual of Oracle in SQL Server?
提问by smilu
Is there any way to create a unique sequence no in SQL Server
有没有办法在 SQL Server 中创建唯一的序列号
select <sequence>.nextval from dual
We have the above code for Oracle which will create a unique number and the number cannot be used by any other.
我们有上面的 Oracle 代码,它将创建一个唯一的编号,并且该编号不能被任何其他人使用。
In the same way how can I create a unique sequence number in SQL Server????
以同样的方式如何在 SQL Server 中创建唯一的序列号???
Some one help me in this
有人帮我解决这个问题
回答by Andrew
Sequences are being added in SQL Server 2012 - but this is not quite released as yet. The release candidates for the product has them in if you want to try them out in advance.
SQL Server 2012 中正在添加序列 - 但目前还没有完全发布。如果您想提前试用,产品的候选发布版本中包含它们。
If the sequence is to just be used by a single table, then you can use the identity column feature, which allows you to specify a starting number and the value added per record.
如果序列只供单个表使用,那么您可以使用标识列功能,它允许您指定起始编号和每条记录添加的值。
If you are looking for a sequence that can be used across a number of tables, you would need to create a dedicated table within SQL server, that has a single column of an identity field - and whenever you wanted a .nextval, you would insert a row into the table to get a new number. The table than acts like a sequence. The table will fill up over time, there is no requirement to keep records within the table, the identity stores it's current value and increment to obtain the next value on a system table.
如果您正在寻找可以跨多个表使用的序列,您需要在 SQL Server 中创建一个专用表,该表具有一个标识字段的单列 - 无论何时您想要一个 .nextval,您都可以插入表中的一行以获得一个新数字。该表的作用就像一个序列。该表会随着时间的推移而填满,不需要在表中保留记录,身份存储它的当前值并递增以获得系统表上的下一个值。
回答by Mithrandir
Only the upcoming SQL Server 2012 supports a sql standard conform sequence object. :-( In any current version of SQL Server (2008 R2 and below) you must use a column with the identity contstraint as in:
只有即将推出的 SQL Server 2012 支持符合 sql 标准的序列对象。:-( 在任何当前版本的 SQL Server(2008 R2 及更低版本)中,您必须使用具有标识约束的列,如下所示:
CREATE TABLE t1
(
id int identity(1,1),
somecol varchar(50)
);
This will generate values in the id column starting with 1 and incrementing them in steps of 1.
这将在 id 列中生成从 1 开始并以 1 为步长递增的值。