My job takes me just about everywhere around this great country of ours and beyond its borders in the quest of helping clients and partners get the best out of their Microsoft Dynamics GP application and data.
In reference to the latter - data - I had been asked recently by a client how they could produce XML formatted data from their Microsoft Dynamics GP databases to be consumed by some web services applications they had developed. Some conditions around this request:
- The client did not want to implement eConnect Requester, though I have to admit this would have been a slam dunk with MSMQ queues.
- The XML documents needed to be rather available and changeable very quickly to serve other needs.
- No additional investments in third party products, middlewares or the likes could be suggested since budget was pretty tight.
The answer could only be one: use the powerful XML capabilities of T-SQL to get data out in the format required by the client.
There's a powerful option when querying data from SQL Server for use with third party applications and/or web services. You can execute SQL queries to return results as XML instead of standard rowsets. These queries can be executed directly or executed from within stored procedures and user-defined functions.
The FOR XML clause has some great benefits:
- It allows a SQL Server developer of Microsoft Dynamics GP consultant to write critical pieces of integration architecture without having to learn the destination system's schema.
- Additional table columns - pieces of data, if you will - can be added to the results with relative ease.
- It's an efficient way to process data and reduces the number of components that must be developed.
- It can be formatted to match target schemas in order to simplify mapping and/or middleware configuration.
Keep in mind that SQL Server has an AUTO mode which allows it to automatically format the XML document for you, relinquishing some control from you the developer or consultant.
For more information on SQL Server FOR XML and the WITH XMLNAMESPACES clauses, please take a look at SQL Server Books Online:
MSDN - FOR XML clause - http://msdn.microsoft.com/en-us/library/ms178107.aspx
MSDN - WITH XMLNAMESPACES clause - http://msdn.microsoft.com/en-us/library/ms177400.aspx
Now a practical application...
This is a simple example on how to implement all of it together. Let's take the case of a Customer with multiple addresses. The following query should produce XML data with our customer master (RM00101) and address master information (RM00102).
The results are pretty straight forward:
|FOR XML output (formatted for display purposes only)|
Nothing but XML greatness!
The above query could have been encapsulated in a stored procedure with a parameter for customer number, which could have driven the results displayed. As you can tell, getting the data you need for any destination, will depend on you specific requirements, but it's doable with the power of SQL Server and T-SQL.
Until next post!
Mariano Gomez, MVP