admin管理员组文章数量:1125068
I created a stored procedure in an Azure SQL database that does something along the lines of:
CREATE PROCEDURE [dbo].[DoSomething](@Script NVARCHAR(MAX))
AS
BEGIN
EXEC (@Script)
END
In another Azure SQL database I created an external data source pointing at this and I run the procedure
EXEC sp_execute_remote @datasource,
N'EXEC [dbo].[DoSomething] @Script',
N'@Script NVARCHAR(MAX)',
@Script = ....
The script can contain various different scripts I want to execute remotely. One example would be
SELECT <some columns> FROM <somewhere>
SELECT <different columns> FROM <somewhereelse>
When I run the script through SSMS directly on the target database, "of course" I get the two result sets.
However when I run the script through the stored procedure using sp_execute_remote
, I only see the first result set in the output grid in SSMS.
The script doesn't appear to be the problem here. The same thing happens when I simplify the script to its bare essentials:
SELECT 'hello world 1'
SELECT 'hello world 2'
The only thing I see in the output grid in SSMS is hello world 1
.
I was also able to confirm that both SELECT
statements are actually executed. I did this using a table variable
DECLARE @outputtable TABLE ([output] NVARCHAR(100))
INSERT INTO @outputtable
EXEC ('SELECT ''hello world 1''')
INSERT INTO @outputtable
EXEC ('SELECT ''hello world 2''')
SELECT * FROM @outputtable
Now it shows both outputs.
But of course, in real life it isn't that simple. In real life, the script may output a number of result sets with very different columns in each.
Question: is there anything I can do to make sure all result sets are returned to the SSMS output grid?
I created a stored procedure in an Azure SQL database that does something along the lines of:
CREATE PROCEDURE [dbo].[DoSomething](@Script NVARCHAR(MAX))
AS
BEGIN
EXEC (@Script)
END
In another Azure SQL database I created an external data source pointing at this and I run the procedure
EXEC sp_execute_remote @datasource,
N'EXEC [dbo].[DoSomething] @Script',
N'@Script NVARCHAR(MAX)',
@Script = ....
The script can contain various different scripts I want to execute remotely. One example would be
SELECT <some columns> FROM <somewhere>
SELECT <different columns> FROM <somewhereelse>
When I run the script through SSMS directly on the target database, "of course" I get the two result sets.
However when I run the script through the stored procedure using sp_execute_remote
, I only see the first result set in the output grid in SSMS.
The script doesn't appear to be the problem here. The same thing happens when I simplify the script to its bare essentials:
SELECT 'hello world 1'
SELECT 'hello world 2'
The only thing I see in the output grid in SSMS is hello world 1
.
I was also able to confirm that both SELECT
statements are actually executed. I did this using a table variable
DECLARE @outputtable TABLE ([output] NVARCHAR(100))
INSERT INTO @outputtable
EXEC ('SELECT ''hello world 1''')
INSERT INTO @outputtable
EXEC ('SELECT ''hello world 2''')
SELECT * FROM @outputtable
Now it shows both outputs.
But of course, in real life it isn't that simple. In real life, the script may output a number of result sets with very different columns in each.
Question: is there anything I can do to make sure all result sets are returned to the SSMS output grid?
Share Improve this question edited 2 days ago Thom A 95.3k11 gold badges59 silver badges92 bronze badges asked 2 days ago DinahMoeHummDinahMoeHumm 5824 silver badges19 bronze badges 2 |1 Answer
Reset to default 2The behaviour you're seeing, with sp_execute_remote
, is documented behaviour. From the the Results set section:
Result set
Returns the result set from the first T-SQL statement.
As a result if your procedure returns many result sets you will only see the first, regardless of the client tool you are using; the problem isn't related to SSMS.
If the procedure returns datasets with the same definition, you could CREATE
a a (temporary) table, INSERT
the data into it and then SELECT
from it. Something like:
DECLARE @stmt nvarchar(MAX),
@CRLF nchar(2) = NCHAR(13) + NCHAR(10);
SET @stmt = N'CREATE TABLE #ProcResults (Col1 varchar(10),' + @CRLF +
N' ...' + @CRLF +
N' Col10 int);' + @CRLF +
N'INSERT INTO #Procresults (Col1, ..., Col10)' + @CRLF +
N'EXEC [dbo].[DoSomething] @Script;' + @CRLF +
N'SELECT * FROM #ProcResults;';
EXEC sp_execute_remote @datasource,
@stmt,
N'@Script NVARCHAR(MAX)',
@Script = ....
If the result sets differ, however, then you will need to separate the separate SELECT
s into their own procedures, and execute each separately.
本文标签: t sqlspexecuteremote only outputs one result setStack Overflow
版权声明:本文标题:t sql - sp_execute_remote only outputs one result set? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736652190a1946164.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
sp_execute_remote
from C#, it would be the same, yes; it's the procedure that has the limitation. – Thom A Commented 2 days ago