获取最后一个插入ID的标准方法是什么? [英] Whats the standard way of getting the last insert id?

查看:121
本文介绍了获取最后一个插入ID的标准方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取最后插入的ID的SQL标准是什么?如果有这样的事情。

What's the sql standard to get the last inserted id? If there is such a thing.

mysql:LAST_INSERT_ID()

postgresql:...返回f_id

mssql:SCOPE_IDENTITY()

...更多示例在这里...

mysql: LAST_INSERT_ID()
postgresql: ... RETURNING f_id
mssql: SCOPE_IDENTITY()
... more examples here ...

我的意思是,所有数据库对此都有不同的实现,对于这样的常见任务没有标准吗?

I mean, all databases have different implementations for that, there isn't a standard for such a common task?

推荐答案

查看此答案检索已插入SQL中的行ID

简而言之,除了MAX(ID)之外,没有跨数据库的方法可以做到这一点-但这不是保证的结果,并且有很多陷阱,例如

In short, there is no cross database way to do this, except MAX(ID) - but that is not a guaranteed result and has many many pitfalls, e.g.


  • 其他插入可以介于最后一次插入和最大查询之间

  • 不能与高事务表一起使用(max将发出读锁定,rdbms特定的方法不会从任何表中读取)

ANSI与身份/自动编号/ auto_increment /顺序有关的标准最早出现在 SQL:2003 等待所有主要RDBMS的实施。它很可能类似于Oracle / PostgreSQL序列。

The ANSI standard that relates to identity/autonumber/auto_increment/sequences first appeared in SQL:2003 awaiting implementation by all major RDBMS. It will most likely resemble Oracle/PostgreSQL sequences.


SQL:2003标准对SQL:1999的所有部分进行了较小的修改(也称为(例如SQL3),并正式引入了一些新功能,例如:

-序列生成器,它允许标准化序列

The SQL:2003 standard makes minor modifications to all parts of SQL:1999 (also known as SQL3), and officially introduces a few new features such as:

- the sequence generator, which allows standardized sequences

SQL:2003中的另一个更改是使用子句的输出,但是关于它的信息很少。 Sybase和SQL Server对其进行了不同的操作,因此目前尚不清楚它将如何推出。 SQL Server将其实现为

Another change in SQL:2003 is the OUTPUT USING CLAUSE but there is very little information about it. Sybase and SQL Server have done different things with it, so it is unclear as yet how it will pan out. SQL Server implements it as

INSERT INTO TBL(..)
OUTPUT inserted.identity_col
INTO @sometablevar
VALUES(..)

这篇关于获取最后一个插入ID的标准方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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