Question

I think this one's pretty straightforward, but I can't seem to find it answered.

I'm running a query on SQL Server 2005 with for xml, elements at the end to return the result as XML.

select top 10 title, pubdate, description, link
from rssfeed item
for xml auto, elements

I'm running the query from a classic ASP page. Normally to get the results from a column, I'd use rs.Fields.Item("colName"). The problem is, with for xml, the column name is a long string like XML_F52E2B61-....

I tried rs.Fields(0).value. That weirdly returned the names of the columns in the query separated by question marks, not the XML. My output is:

?title?pubdate?description?link?item?L???

I tested my query in Management Studio, and the XML looks alright. Is there a way to read this value into classic ASP?

Was it helpful?

Solution

Found the answer in SQL Server forum. There's probably a cleaner way to do this, but the idea from the post is to convert the XML to a varchar(max).

DECLARE @xmlout as varchar(max);
DECLARE @X as Xml;

set @X = (select top 10 title, pubdate, description, link
from rssfeed item
for xml auto, elements);

select @xmlout = cast(@X as varchar(max));
select @xmlout;

After running the query, I'm then using rs(0) to read the returned value.

OTHER TIPS

The other answer by @dangowans is very good. I reached here searching for a solution for a similar problem.

However, I could manage without moving to a stored procedure kind of thing and managed using a single query like this:

Select k=(select top 10 title, pubdate, description, link
from rssfeed item
for xml auto, elements)

And it returned me the XML output as varchar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top