Oracle序列,但随后在MS SQL Server中 [英] Oracle sequence but then in MS SQL Server

查看:70
本文介绍了Oracle序列,但随后在MS SQL Server中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Oracle中,有一种生成序列号的机制,例如;

In Oracle there is a mechanism to generate sequence numbers e.g.;

CREATE SEQUENCE supplier_seq

    MINVALUE 1
    MAXVALUE 999999999999999999999999999
    START WITH 1
    INCREMENT BY 1
    CACHE 20;

然后执行该语句

supplier_seq.nextval

检索下一个序列号.

您将如何在MS SQL Server中创建相同的功能?

How would you create the same functionality in MS SQL Server ?

我不是在寻找自动为表记录生成键的方法.我需要生成一个可用作进程的(逻辑)ID的唯一值.因此,我需要Oracle提供的确切功能.

I'm not looking for ways to automaticly generate keys for table records. I need to generate a unique value that I can use as an (logical) ID for a process. So I need the exact functionality that Oracle provides.

推荐答案

没有完全匹配的内容.

等效项是IDENTITY,您可以在创建表时将其设置为数据类型. SQLSERVER将在插入过程中自动创建一个运行序列号. 可以通过调用SCOPE_IDENTITY()或通过查询系统变量@@ IDENTITY(由Frans指出)来获取最后插入的值

The equivalent is IDENTITY that you can set as a datatype while creating a table. SQLSERVER will automatically create a running sequence number during insert. The last inserted value can be obtained by calling SCOPE_IDENTITY() or by consulting the system variable @@IDENTITY (as pointed out by Frans)

如果需要精确的等效项,则需要创建一个表,然后编写一个过程以重新调整下一个值和其他操作.请参阅Marks关于此方面的陷阱.

If you need the exact equivalent, you would need to create a table and then write a procedure to retun the next value and other operations. See Marks response on pitfalls on this.


SQL Server实现了类似于Oracle的序列.有关更多详细信息,请参考此问题.


SQL Server has implemented the Sequence similar to the Oracle. Please refer to this question for more details.

如何在Microsoft SQL Server中实现序列? /a>

How would you implement sequences in Microsoft SQL Server?

这篇关于Oracle序列,但随后在MS SQL Server中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆