Getting Last Id from a Cross Server Insert using SQL Server(使用 SQL Server 从跨服务器插入获取最后一个 ID)
问题描述
我正在运行跨服务器插入
I am running a cross server insert
INSERT INTO server.database.dbo.table (Field) VALUES('test')
之后,我需要获取最后一次插入的 id.但是,当我运行 scope_identity 函数时,我没有从外部服务器获取最新的 id.
Afterward, I need to get the id of the last insert. However, when I run the the scope_identity function, I don't get the latest id from the foreign server.
SELECT @ID=SCOPE_IDENTITY()
如何从跨服务器插入中检索最后一个 ID?
How would I retrieve the last id from a cross server insert?
推荐答案
来自 http://msdn.microsoft.com/en-us/library/ms187342.aspx
@@IDENTITY 函数的作用域是本地服务器上的当前会话在其上执行.这个功能不能应用于远程或链接服务器.获取身份值在不同的服务器上,执行该远程存储过程或链接服务器并存储过程(在远程或链接的上下文服务器)收集身份值和将其返回到调用连接本地服务器.
The scope of the @@IDENTITY function is current session on the local server on which it is executed. This function cannot be applied to remote or linked servers. To obtain an identity value on a different server, execute a stored procedure on that remote or linked server and have that stored procedure (which is executing in the context of the remote or linked server) gather the identity value and return it to the calling connection on the local server.
这篇关于使用 SQL Server 从跨服务器插入获取最后一个 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 SQL Server 从跨服务器插入获取最后一个 ID


- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- SQL 临时表问题 2022-01-01