从 exec 存储过程中获取 xml

Getting xml as result from exec stored procedure

我正在尝试将执行的存储过程的结果检索为 table 的 XML 结果显示为原来的结果。

我正在尝试做的是这样的事情。

exec dbo.StoredProcedure FOR XML RAW, ROOT ('root_name').

假设在存储过程中执行 dbo.StoredProcedure returns table, 我想要 FOR XML RAW, ROOT ('root_name') 到 return 整个结果的 XML 值。

如何在 SQL 服务器中实现此目的?

一种方法是使用 INSERT...EXEC 将 proc 结果插入到临时 table 或变量中,然后使用所需的 FOR XML 从那个 table select ] 查询:

DECLARE @results AS TABLE(col1 int, col2 int);
INSERT INTO @results EXEC dbo.StoredProcedure;
SELECT col1, col2
FROM @results
FOR XML RAW, ROOT ('root_name');

根据 this question, you should not select from the stored procedure. Also if you dig into that question, you will find a link to an interesting article 描述您对存储过程的选择。

如果我是你,我会 return 带有输出参数的 XML,或者将 insert-exec 插入 table 然后用 for xml 查询它.