gpt4 book ai didi

sql-server - 使用 SQL Server 从跨服务器插入中获取最后一个 ID

转载 作者:行者123 更新时间:2023-12-03 18:00:31 25 4
gpt4 key购买 nike

我正在运行一个跨服务器插入

INSERT INTO server.database.dbo.table (Field) VALUES('test')

之后,我需要获取最后插入的 ID。但是,当我运行 scope_identity 函数时,我没有从外部服务器获取最新的 ID。

SELECT @ID=SCOPE_IDENTITY()

我如何从跨服务器插入中检索最后一个 ID?

最佳答案

来自 http://msdn.microsoft.com/en-us/library/ms187342.aspx

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 - 使用 SQL Server 从跨服务器插入中获取最后一个 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/827118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com