开发者

Saving the XML results to variable or table in SQL

How can I save a XML to variable or table which is generated by SQL stored procedure?

Here's my sample:

DECLARE @ReportResultXml TABLE(RXml XML)    
DECLARE @ResultXml  XML = NULL

INSERT @ReportResultXml EXEC (@Script + ' FOR XML RAW, ROOT, XMLSCHEMA, ELEMENTS')
SELECT @ResultXml = RXml FROM @ReportResultXml
EXEC Repor.ProcInsert 1, @ResultXml

SELECT @ResultXml

The @Script variable contains this SQL query:

DECLAR开发者_JAVA百科E @PeriodID INT
SET @PeriodID = 12
SELECT TOP 10 PeriodID, Name
FROM Agent.Points
WHERE PeriodID = @PeriodID

That statement will generate the following error:

The FOR XML clause is not allowed in a INSERT statement.


This may help you to find your solution:

declare @script nvarchar(max)
set @script = 'select 1 as a, 2 as b for xml raw'

declare @ReportResultXml table (RXml XML)
declare @ResultXml xml

declare @sql nvarchar(max)

set @sql = 'set @ResultXml = (' + @script + ')'
execute sp_ExecuteSQL @sql, N'@ResultXml xml output', @ResultXml output

insert into @ReportResultXml select @ResultXml

select * from @ReportResultXml
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜