問題描述
我正在運(yùn)行跨服務(wù)器插入
I am running a cross server insert
INSERT INTO server.database.dbo.table (Field) VALUES('test')
之后,我需要獲取最后一次插入的 id.但是,當(dāng)我運(yùn)行 scope_identity 函數(shù)時(shí),我沒有從外部服務(wù)器獲取最新的 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()
如何從跨服務(wù)器插入中檢索最后一個(gè) ID?
How would I retrieve the last id from a cross server insert?
推薦答案
來(lái)自 http://msdn.microsoft.com/en-us/library/ms187342.aspx
@@IDENTITY 函數(shù)的作用域是本地服務(wù)器上的當(dāng)前會(huì)話在其上執(zhí)行.這個(gè)功能不能應(yīng)用于遠(yuǎn)程或鏈接服務(wù)器.獲取身份值在不同的服務(wù)器上,執(zhí)行該遠(yuǎn)程存儲(chǔ)過程或鏈接服務(wù)器并存儲(chǔ)過程(在遠(yuǎn)程或鏈接的上下文服務(wù)器)收集身份值和將其返回到調(diào)用連接本地服務(wù)器.
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.
這篇關(guān)于使用 SQL Server 從跨服務(wù)器插入獲取最后一個(gè) ID的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!