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
基础教程推荐
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
