Subscribe to Subscribers
Talk to Webmaster Tony John

Resources » .NET programming » Databases

SQL XML - Working with tables with unknown columns


Posted Date:     Category: Databases    
Author: Member Level: Gold    Points: 10



 


SQL XML enables you to convert the from a stored procedure/user defined function into XML format. Using this functionality, you can work with stored procedures or functions without needing to know the table column information being returned.

User defined functions


Here is a sample query which will execute the function and convert the table returned into a XML format,

DECLARE @Data XML
SET @Data = (SELECT * FROM dbo.yourFunctionName() FOR XML RAW(''Row''),ROOT(''Root''))


Assuming, yourFunctionName return a table with 3 columns i.e. Name, Age and Gender, the resulting output will be,

<Root>
<Row Namel="ABitSmart" Age="27" Gender="Male" />
</Root>


Stored procedure


With a stored procedure it is little bit tricky. The best way I figured out is, having the stored procedure return a XML parameter as an output. The XML parameter can be created in the same way as we did for the function.

To convert a table (temporary or in memory) into XML,

-- converting a temporary table to XML format
DECLARE @Data XML
Set @Data = (Select * from #tempTable For XML RAW('Row'),ROOT('Root'))

Or

-- converting an in-memory table to XML format
DECLARE @Data XML
Set @Data = (Select * from @tempTable For XML RAW('Row'),ROOT('Root'))


The above approach shows that we do not have to bother with the column details of table being returned. The XML will have all this metadata. Every node of the XML will corresponds to a table row. Each row will have the corresponding column value pair as the attribute value.

I have used this concept in creating a generic reporting architecture. There is one main executing stored procedure which takes in the procedure name and returns a XML output. LINQ calls this procedure, passing the stored procedure name. The main procedure executes(Dynamic SQL) the passed in stored procedure name (which is a reporting procedure) and converts the output table into a XML output. This XML output is returned to the main application.
The advantage is a loosely coupled architecture. The report procedures can be altered without having the application to be involved.


Have fun.

Reference http://abitsmart.com/2009/11/sql-returning-table-with-unknown-columns/




Read related articles: Sql Xml    


Did you like this resource? Share it with your friends and show your love!


Responses to "SQL XML - Working with tables with unknown columns"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Joining Tables in SQL
    Previous Resource: Triggers in SQL
    Return to Resources
    Post New Resource
    Category: Databases


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Converting table to XML  .  Returning table with unknown columns  .  SQL XML  .  
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.