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.
I have seen this post but I can't add any stored procedures in remote sql server
You could use the remote side's sp_executesql
:
DECLARE @ScopeIdentity TABLE (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;
Alternatively, you could use OPENQUERY
:
SELECT *
FROM OPENQUERY(server, '
INSERT INTO database.schema.table (columns) VALUES (values);
SELECT SCOPE_IDENTITY() AS ID');