如何在PHP的MSSQL中获取插入ID? [英] How to get Insert id in MSSQL in PHP?

查看:79
本文介绍了如何在PHP的MSSQL中获取插入ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题标题怎么说.对于诸如SELECT @@IDENTITY AS ins_id之类的查询,我是否需要提供表名或任何其他信息以指定我正在谈论的表/数据库?

What the question title says. With a query such as SELECT @@IDENTITY AS ins_id, do I need to supply the table name or any other info to specify which table/database i'm talking about?

推荐答案

@@IDENTITY 返回当前会话中生成的最新身份.在大多数情况下,您可能希望使用 SCOPE_IDENTITY ,它返回当前范围内生成的最新标识.

@@IDENTITY returns the most recent identity generated in the current session. In most cases you'll probably want to use SCOPE_IDENTITY instead, which returns the most recent identity generated in the current scope.

例如,如果您在 table1 中插入一行,但是该插入触发了将行插入 table2 中的触发器,那么<​​c1>将从 table2 ,而SCOPE_IDENTITY将从 table1 返回身份.

For example, if you insert a row into table1, but that insert fires a trigger which inserts a row into table2, then @@IDENTITY will return the identity from table2 whereas SCOPE_IDENTITY will return the identity from table1.

INSERT INTO my_table (my_column) VALUES ('test')

-- return the identity of the row you just inserted into my_table
-- regardless of any other inserts made by triggers etc
SELECT SCOPE_IDENTITY() AS ins_id

这篇关于如何在PHP的MSSQL中获取插入ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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