1

I would like to declare a variable 'XMLOutput' and have it produce the contents of a table in XML format. If you could provide a really simple example I could work off of I would really appreciate it. I tried using the xmlelement() but could not get it to work.

1
  • for xml auto, Elements will put data into correct xml format for me Commented Nov 15, 2010 at 20:27

3 Answers 3

2

SQL Server provides the ability to generate XML based on table structure via the FOR XML clause. It's options are:

  • RAW
  • AUTO
  • PATH
  • EXPLICIT

There are examples for each in the link.

Sign up to request clarification or add additional context in comments.

2 Comments

Just out of curiosity, do you know an easy way to do the reverse? I need to import a large XML file (actually a SOAP file) and currently use a bloated SSIS structure for it...
@littlegreen: That's called "shredding" - here's an article on it for SQL Server 2005
1

Try using

FOR XML RAW

In the end of your query. This will return results as XML. Does that do what you want ? If not, I think you might need to elaborate your question a bit further. You could also have a look at the documentation, to see what options you have with FOR XML.

2 Comments

Select xmlelement(Loc, 'Loc', g.Loc) from sitemap sm for xml raw
Select top 5 priority from ghhsitemap g for xml raw this produces xml but in wrong format(<row priority='.9'/>, I just need <Priority>.9</priority>
0
create xml schema collection cricketschemacollection
AS N'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLschema">
<xsd:element name="MatchDetails">
<xsd:complexType>
<xsd::complexContent>
<xsd:restiriction base="xsd:anyType">
<xsd:sequences>
<xsd:element name="Team" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd::complexContent>
<xsd:restiriction base="xsd:anyType">
<xsd:sequences/>
<xsd:attribute name="country"type="xsd:string"/>
<xsd:attribute name="score"type="xsd:string"/>
</xsd:restiriction>
</xsd::complexContent>
</xsd:complexType>
</xsd:element>
</xsd:sequences>
</xsd:restiriction>
</xsd::complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>'

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.