問題描述
我正在運行跨服務器插入
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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!