Generating XML

  • How do I generate XML from a query in SQL Server, if its possible?

  • You should look for "FOR XML" clause in BOL or search this site for "XML Workshop Sebastian". The latter will result in a list of great articles by Jacob Sebastian.

    Or you could provide some ready to use sample data as per the first link in my signature together with your expected result set and there will be several people around to show you what needs to be done (if possible, of course)...



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Assuming you are on SQL Server 2005, here is a very simple example of what Lutz was talking about:

    SELECT *

    FROM INFORMATION_SCHEMA.TABLES

    FOR XML PATH

    If you execute this with "Results to grid" in Management Studio, you will see one row with one column, that has a hyperlink in it. If you click on that hyperlink, it will take you to an expanded XML window that will show you the whole thing, formatted and indented.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thank you guys, I am going through Jacb sebastians workshop and his tutorials are just phenomenal. Thanks

  • Glad you found it as useful as I did!

    If you have any specific questions let us know and we'll try to find an answer 😉



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Folks, I want to generate XML for a basic structure where there is several differing element types listed within the root with no relationships.

    Say I have products, customers and offices tables

    I want to just query each table and have each set of elements listed within the document with no parent-child relationships.

    <?xml version="1.0" encoding="UTF-8"?>

    <THISISTHEROOT>

    <Customer>

    <firstName>Jim</firstName>

    <lastName>Flim</lastName>

    </Customer>

    <Customer>

    <firstName>Bob</firstName>

    <lastName>Flob</lastName>

    </Customer>

    <product>

    <productname> SoapPowder</productname>

    <price>50</productname>

    </product>

    <product>

    <productname> CheeseFlour</productname>

    <price>20</productname>

    </product>

    <office>

    <address> 15 ship lane </address>

    <owner>Davys properties>

    </office>

    <office>

    <address> 15 turnip lane </address>

    <owner>Jones properties>

    </office>

    </THISISTHEROOT>

    How do I construct a FOR XML statement where the ordinary SQL statement would involve just calling

    select * from products

    select * from customers

    select * from offices

    but have these in XML and within a root element.

    Thanks

    Mick

  • mick L (2/15/2010)


    Folks, I want to generate XML for a basic structure where there is several differing element types listed within the root with no relationships.

    Say I have products, customers and offices tables

    I want to just query each table and have each set of elements listed within the document with no parent-child relationships.

    <?xml version="1.0" encoding="UTF-8"?>

    <THISISTHEROOT>

    <Customer>

    <firstName>Jim</firstName>

    <lastName>Flim</lastName>

    </Customer>

    <Customer>

    <firstName>Bob</firstName>

    <lastName>Flob</lastName>

    </Customer>

    <product>

    <productname> SoapPowder</productname>

    <price>50</productname>

    </product>

    <product>

    <productname> CheeseFlour</productname>

    <price>20</productname>

    </product>

    <office>

    <address> 15 ship lane </address>

    <owner>Davys properties>

    </office>

    <office>

    <address> 15 turnip lane </address>

    <owner>Jones properties>

    </office>

    </THISISTHEROOT>

    How do I construct a FOR XML statement where the ordinary SQL statement would involve just calling

    select * from products

    select * from customers

    select * from offices

    but have these in XML and within a root element.

    Thanks

    Mick

    Two things: First, you need to post your question as a separate thread in this forum, so that it can receive the attention it deserves.

    Secondly, when you do, please use the [ code="xml" ] tags to better format your XML code, with indentation, without having to entitize it first.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Here's an example using the [ code="xml" ] tag. If you want to see exactly how it is done, just hit the "Quote" button over my post:

    <TABLES>

    <TABLE_CATALOG>NewTest1</TABLE_CATALOG>

    <TABLE_SCHEMA>dbo</TABLE_SCHEMA>

    <TABLE_NAME>SchemasPk</TABLE_NAME>

    <TABLE_TYPE>BASE TABLE</TABLE_TYPE>

    </TABLES>

    <TABLES>

    <TABLE_CATALOG>NewTest1</TABLE_CATALOG>

    <TABLE_SCHEMA>dbo</TABLE_SCHEMA>

    <TABLE_NAME>varchar_test</TABLE_NAME>

    <TABLE_TYPE>BASE TABLE</TABLE_TYPE>

    </TABLES>

    <TABLES>

    <TABLE_CATALOG>NewTest1</TABLE_CATALOG>

    <TABLE_SCHEMA>dbo</TABLE_SCHEMA>

    <TABLE_NAME>tblArticle</TABLE_NAME>

    <TABLE_TYPE>BASE TABLE</TABLE_TYPE>

    </TABLES>

    <TABLES>

    <TABLE_CATALOG>NewTest1</TABLE_CATALOG>

    <TABLE_SCHEMA>dbo</TABLE_SCHEMA>

    <TABLE_NAME>DemoTable1</TABLE_NAME>

    <TABLE_TYPE>BASE TABLE</TABLE_TYPE>

    </TABLES>

    <TABLES>

    <TABLE_CATALOG>NewTest1</TABLE_CATALOG>

    <TABLE_SCHEMA>dbo</TABLE_SCHEMA>

    <TABLE_NAME>Timestamp_test</TABLE_NAME>

    <TABLE_TYPE>BASE TABLE</TABLE_TYPE>

    </TABLES>

    <TABLES>

    <TABLE_CATALOG>NewTest1</TABLE_CATALOG>

    <TABLE_SCHEMA>dbo</TABLE_SCHEMA>

    <TABLE_NAME>DemoTable2</TABLE_NAME>

    <TABLE_TYPE>BASE TABLE</TABLE_TYPE>

    </TABLES>

    <TABLES>

    <TABLE_CATALOG>NewTest1</TABLE_CATALOG>

    <TABLE_SCHEMA>dbo</TABLE_SCHEMA>

    <TABLE_NAME>Demo_UDFVFM02</TABLE_NAME>

    <TABLE_TYPE>VIEW</TABLE_TYPE>

    </TABLES>

    <TABLES>

    <TABLE_CATALOG>NewTest1</TABLE_CATALOG>

    <TABLE_SCHEMA>dbo</TABLE_SCHEMA>

    <TABLE_NAME>vwTableInfo</TABLE_NAME>

    <TABLE_TYPE>VIEW</TABLE_TYPE>

    </TABLES>

    See, isn't that better?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Apologies. As my question follows the context of this existing thread, I assumed that appending to the thread would be preferable to proliferating similar threads.

    Thanks for the XML tag tip.

    Can anyone help me with shaping this XML?

  • mick L (2/15/2010)


    Apologies. As my question follows the context of this existing thread, I assumed that appending to the thread would be preferable to proliferating similar threads. ...

    Nope, proliferating threads is not a problem here, as long as it's one thread per question.

    Extending threads with different questions, even if they share the same general topic, IS a problem, because it becomes confusing, and because many of those who could help look for New threads, and may not jump in on an old thread that they are not arlready active in.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply