如何在PHP的MSSQL中获取插入ID? [英] How to get Insert id in MSSQL in PHP?
问题描述
问题标题怎么说.对于诸如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屋!