Du kan använda fjärrsidans 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;
Alternativt kan du använda OPENQUERY
:
SELECT *
FROM OPENQUERY(server, '
INSERT INTO database.schema.table (columns) VALUES (values);
SELECT SCOPE_IDENTITY() AS ID');