在 SELECT 将返回记录的情况下,INSERT 不会返回结果集,但受影响的记录数除外。这可以通过使用 SET NOCOUNT ON 来抑制;但是,我不确定抑制是否是指可见性或实际出现的行数。
INSERT INTO OPENQUERY(MYSERVER, 'SELECT [Drive_Letter] ,[MBFree],[Server] FROM [Blah].[dbo].[SQL_Drives]')
SELECT 'X', 2, 'MyServer'
(1 row(s) affected)
至于从 INSERT 返回的记录,实现这一点的唯一方法是使用 OUTPUT 子句。客户端机器将无权访问 OUTPUT INSERTED 行,因此无法返回这些行。如果您尝试运行以下命令,您将收到错误消息:
INSERT INTO OPENQUERY(MYSERVER, 'SELECT [Drive_Letter] ,[MBFree],[Server] FROM [BLAH].[dbo].[SQL_Drives]')
OUTPUT INSERTED.*
SELECT 'X', 2, 'MyServer'
Msg 405, Level 16, State 1, Line 1
A remote table cannot be used as a DML target in a statement which includes an OUTPUT clause or a nested DML statement.
-- EDIT RESULTS OF PROFILER ---------------------------
-- Statements that occured on server called through OPENQUERY
exec sp_cursoropen @p1 output,N'SELECT [Drive_Letter] ,[MBFree],[Server] FROM [MyServer].[dbo].[SQL_Drives]',@p3 output,@p4 output,@p5 output
select @p1, @p3, @p4, @p5
exec sp_cursor 180150009,4,0,N'[MyServer].[dbo].[SQL_Drives]',@Drive_Letter='X',@MBFree=2,@Server='MyServer'
--Statements that occured on client
INSERT INTO OPENQUERY(MyServer, 'SELECT [Drive_Letter] ,[MBFree],[Server] FROM [MyServer].[dbo].[SQL_Drives]')
SELECT 'X', 2, 'MyServer'