Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql query to produce xml output

I have these tables

Table 1 tbl1_site [facilityId] [name]

Table 2 tbl2_applicant [pvid] [facilityId] [npi] [firstname]

FK join key: tbl1_site.facilityId = tbl2_applicant.facilityId

Table 3 tbl3_abstraction [pvid] [patientnumber] [diabetesdiagnosis] [dateofbirth]

FK join key: tbl2_applicant.pvId = tbl3_abstraction.pvId

i have problem to create a sql query to reproduce this xml output.

thanks

<account>
    <metadata />
    <practice-sites>
        <practice-site>
            <metadata>
                <data-element id="name">
                    <value>My Own Diabetes Medical Center</value>
                </data-element>
            </metadata>
            <applicants>
                <metadata />
                <applicant>
                    <metadata>
                        <data-element id="npi">
                            <value>1234567890</value>
                        </data-element>
                        <data-element id="firstname">
                            <value>Joseph</value>
                        </data-element>
                    </metadata>
                    <clinical-abstractions>                           
                        <clinical-abstraction>
                            <data-element id="diabetesdiagnosis">
                                <value>Backward</value>
                            </data-element>
                            <data-element id="dateofbirth">
                                <value>02/01/2009</value>
                            </data-element>
                            <data-element id="patientnumber">
                                <value>1</value>
                            </data-element>
                        </clinical-abstraction>
                    </clinical-abstractions>
                </applicant>
            </applicants>
        </practice-site>
    </practice-sites>
</account>
like image 826
Maged Samaan Avatar asked Feb 03 '12 20:02

Maged Samaan


People also ask

How can I get SQL query results in XML?

You can optionally retrieve formal results of a SQL query as XML by specifying the FOR XML clause in the query. The FOR XML clause can be used in top-level queries and in subqueries. The top-level FOR XML clause can be used only in the SELECT statement.

Can you convert SQL to XML?

With SQL Server Management Studio, you can save your SQL database as CSV and then you can convert the database files to XML, PDF or other formats as you like.


2 Answers

do you really need all those tags? I mean the "metadata" and "data-element"

try this query, it shows the data on the format you need:

select  t1.name as PracticeSite,
        (SELECT t2.npi as NPI, 
                t2.firstname,
                (SELECT t3.patientnumber, 
                        t3.diabetesdiagnosis, 
                        t3.dateofbirth
                        FROM tbl3_abstraction t3
                        WHERE t3.pvId=t2.pvId
                        FOR XML PATH('clinical-abstraction'), TYPE
                        ) as 'clinical-abstractions'
                FROM tbl2_applicant t2
                WHERE t1.[facilityId]=t2.[facilityId]
                FOR XML PATH('Applicant'), TYPE
        ) AS 'Applicants'
from tbl1_site t1
FOR XML path('PracticeSites'), root('account'), ELEMENTS;
like image 173
Diego Avatar answered Oct 13 '22 00:10

Diego


I think the simplest thing to do would be craft your SQL statement using "SELECT [...] FROM whatever AS XML" to get the data as the default SQL Server generated XML. Once you have that, transform it to your required XML format using XSLT. This could be done via a SQL Server function or a function in your .NET application.

like image 33
Adrian J. Moreno Avatar answered Oct 13 '22 00:10

Adrian J. Moreno