# Export SQL Server as XML

**URL:** <https://forum.xojo.com/t/export-sql-server-as-xml/45383>\
**Category:** Databases\
**Created:** [September 18, 2018, 2:59pm UTC](https://forum.xojo.com/t/export-sql-server-as-xml/45383 "2018-09-18T14:59:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eduardo\_Gutierrez\_de\_Oliveira](https://forum.xojo.com/user_avatar/forum.xojo.com/eduardo_gutierrez_de_oliveira/32/264_2.png) [@Eduardo\_Gutierrez\_de\_Oliveira](https://forum.xojo.com/u/Eduardo_Gutierrez_de_Oliveira)\
**Post date:** [September 18, 2018, 2:59pm UTC](https://forum.xojo.com/t/export-sql-server-as-xml/45383/1 "2018-09-18T14:59:42Z")

</div>

In MS Access VBA there’s a routine called exportXML that allows to redirect tables and/or queries to be output as XML.

It’s not particularly intuitive or efficient, but it’s what’s been used here for a while.

I’m looking at porting these to Xojo and I wondered, before I tried anything, if there’s any built-in export to XML from a SQL recordset.

I imagine not, from a quick search, but it’s one of those thing you’d rather not reinvent the wheel.

This is a reference to the typical way of doing this in Access VBA: [http://www.brileigh.com/microsoft-access/export-xml-data-from-microsoft-access-part-2-using-vba/](http://www.brileigh.com/microsoft-access/export-xml-data-from-microsoft-access-part-2-using-vba/)

Essentially tables with relations or queries can be pushed through the exportXML routines, and even include nested containers as needed.

---

<div class="post-metadata">

**Author:** ![James\_Dooley](https://forum.xojo.com/user_avatar/forum.xojo.com/james_dooley/32/164_2.png) [@James\_Dooley](https://forum.xojo.com/u/James_Dooley)\
**Post date:** [September 18, 2018, 3:15pm UTC](https://forum.xojo.com/t/export-sql-server-as-xml/45383/2 "2018-09-18T15:15:11Z")

</div>

I used the Einhugur Excel Writer to do this - A routine that took a RecordSet and a FolderItem as params and populated the XLS sheet with the data from the RecordSet. It is easy enough to get column names and types from the RecordSet.

---

<div class="post-metadata">

**Author:** ![Eduardo\_Gutierrez\_de\_Oliveira](https://forum.xojo.com/user_avatar/forum.xojo.com/eduardo_gutierrez_de_oliveira/32/264_2.png) [@Eduardo\_Gutierrez\_de\_Oliveira](https://forum.xojo.com/u/Eduardo_Gutierrez_de_Oliveira)\
**Post date:** [September 18, 2018, 3:26pm UTC](https://forum.xojo.com/t/export-sql-server-as-xml/45383/3 "2018-09-18T15:26:29Z")

</div>

> [@406244:@James Dooley](#):
>
> I used the Einhugur Excel Writer to do this - A routine that took a RecordSet and a FolderItem as params and populated the XLS sheet with the data from the RecordSet. It is easy enough to get column names and types from the RecordSet.

I have Einhugur. I’ll check it out, although I need the output in XML proper, rather than XLS.

---

<div class="post-metadata">

**Author:** ![James\_Dooley](https://forum.xojo.com/user_avatar/forum.xojo.com/james_dooley/32/164_2.png) [@James\_Dooley](https://forum.xojo.com/u/James_Dooley)\
**Post date:** [September 18, 2018, 6:14pm UTC](https://forum.xojo.com/t/export-sql-server-as-xml/45383/4 "2018-09-18T18:14:36Z")

</div>

Sorry my mistake, I read XLS instead of XML. If you are using SQL Server it can return the result set as XML:

```auto
USE AdventureWorks2012  
GO  
SELECT Cust.CustomerID,   
       OrderHeader.CustomerID,  
       OrderHeader.SalesOrderID,   
       OrderHeader.Status  
FROM Sales.Customer Cust   
INNER JOIN Sales.SalesOrderHeader OrderHeader  
ON Cust.CustomerID = OrderHeader.CustomerID  
FOR XML AUTO  
```

---

<div class="post-metadata">

**Author:** ![Eduardo\_Gutierrez\_de\_Oliveira](https://forum.xojo.com/user_avatar/forum.xojo.com/eduardo_gutierrez_de_oliveira/32/264_2.png) [@Eduardo\_Gutierrez\_de\_Oliveira](https://forum.xojo.com/u/Eduardo_Gutierrez_de_Oliveira)\
**Post date:** [September 18, 2018, 9:50pm UTC](https://forum.xojo.com/t/export-sql-server-as-xml/45383/5 "2018-09-18T21:50:57Z")

</div>

[quote=406275:@James Dooley]Sorry my mistake, I read XLS instead of XML. If you are using SQL Server it can return the result set as XML:

`
USE AdventureWorks2012  
GO  
SELECT Cust.CustomerID,   
       OrderHeader.CustomerID,  
       OrderHeader.SalesOrderID,   
       OrderHeader.Status  
FROM Sales.Customer Cust   
INNER JOIN Sales.SalesOrderHeader OrderHeader  
ON Cust.CustomerID = OrderHeader.CustomerID  
FOR XML AUTO  
`[/quote]

Ah, nice. I’ll look into this.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 7:22pm UTC](https://forum.xojo.com/t/export-sql-server-as-xml/45383/6 "2020-10-29T19:22:31Z")

</div>


