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
基础教程推荐
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 在多列上分布任意行 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
