2

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?

6

2 Answers 2

3

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.