sha*_*dov 4 sql sql-server-2008
这有效,返回结果集:
exec ('select ''col'', count(1) from test.dbo.[Table1] with (nolock)') at svrA
Run Code Online (Sandbox Code Playgroud)
当我尝试将结果集插入表中时:
insert into rowcount_sub (tablename,rowcnt)
exec ('select ''col'', count(1) from test.dbo.[Table1] with (nolock)') at svrA
Run Code Online (Sandbox Code Playgroud)
失败给出这个错误:
OLE DB provider "SQLNCLI10" for linked server "svrA" returned message "No transaction is active.".
Msg 7391, Level 16, State 2, Line 1
The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "svrA" was unable to begin a distributed transaction.
Run Code Online (Sandbox Code Playgroud)
小智 5
通过使用OPENQUERY而不是EXEC,我能够解决同样的问题:
insert into rowcount_sub (tablename,rowcnt)
SELECT * FROM OPENQUERY(svrA, 'select ''col'', count(1) from test.dbo.[Table1] with (nolock)')
Run Code Online (Sandbox Code Playgroud)
希望它可以帮助某人......