获取链接服务器中插入行的身份的最佳方法? [英] Best way to get identity of inserted row in Linked server?
本文介绍了获取链接服务器中插入行的身份的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用链接服务器在远程Sql Server中插入记录,现在我想获取插入记录的ID.类似于本地服务器中的scope_identity()
.
我的远程SQL Server是2000版本.
I am inserting record in a remote Sql Server using Linked server, Now I wanna get the id of inserted record. something like scope_identity()
in local server.
My remote sql server is 2000 version.
我看过这篇文章,但是我无法添加任何已存储的内容远程SQL Server中的过程
I have seen this post but I can't add any stored procedures in remote sql server
推荐答案
您可以使用远程端的sp_executesql
:
DECLARE @ScopeIdentity (ID int);
INSERT INTO @ScopeIdentity
EXEC server.master..sp_executesql N'
INSERT INTO database.schema.table (columns) VALUES (values);
SELECT SCOPE_IDENTITY()';
SELECT * FROM @ScopeIdentity;
或者,您可以使用OPENQUERY
:
SELECT *
FROM OPENQUERY(server, '
INSERT INTO database.schema.table (columns) VALUES (values);
SELECT SCOPE_IDENTITY() AS ID');
这篇关于获取链接服务器中插入行的身份的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文