在 Interbase 2007 中获取新插入记录标识的命令
在Interbase中(我使用的是2007,我不知道这是否重要)是否有一个命令来获取新插入的记录的标识,类似于SQL Server中的SCOPE_IDENTITY()?
In Interbase (I'm using 2007, I don't know if it matters) is there a command to get the identity of a newly-inserted record, similar to SCOPE_IDENTITY() in SQL Server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,InterBase 并不真正具有身份功能。
相反,InterBase 具有生成器功能。生成器有点像身份,但它们在逻辑上与主键列分开。换句话说,生成器将为您提供一个有保证的唯一值,但您如何处理该值取决于您。
您可以将该值用作单个表或多个表的主键值。但实际上分配主键值是您必须自己完成的事情。
除了没有像
SCOPE_IDENTITY
这样的功能外,InterBase 没有任何从INSERT
语句返回值的功能。因此,您不仅无法从INSERT
语句中获取生成的主键值,也无法获取任何其他值,例如触发器设置的值。解决方法
一种可能的解决方法是提前生成主键值。所以你可以做类似下面的事情(我将在这个例子中使用 InterBase 存储过程语法,因为我不知道你正在使用什么编程语言,但你可以在任何编程语言中做同样的事情):
< code>RDB$DATABASE 是一张只有一条记录的系统表。知道
ID
的值,您可以从过程中返回它。第二种解决方法是使用备用键
SELECT
记录并以这种方式读取生成的 ID。No, InterBase doesn't really have an identity feature.
What InterBase has, instead, is a generator feature. Generators are kind of like identity, but they are logically separated from the primary key column. A generator, in other words, will give you a guaranteed unique value, but what you do with that value is up to you.
You could use that value as the primary key values for a single table, or for multiple tables. But actually assigning the primary key value is something you must do yourself.
In addition to not having a feature like
SCOPE_IDENTITY
, InterBase does not have any kind of feature to return values from anINSERT
statement. So not only can you not get a generated primary key value back from anINSERT
statement, you also cannot get any other values, such as values set by a trigger.Workarounds
One possible workaround for this is to generate the primary key value in advance. So you could do something like the following (I'm going to use InterBase stored procedure syntax for this example, since I don't know what programming language you are using, but you can do the same thing in any programming language):
RDB$DATABASE
is a system table which has only one record. Knowing the value ofID
, you can return it from the proc.A second workaround is to
SELECT
the record using an alternate key and read the generated ID that way.