使用SQL Server的PATH模式嵌套FOR XML结果

时间:2015-11-18 20:05:28

标签: sql-server xml ssis

我正在创建一个SSIS包,我从表中提取数据,需要使用这些列将其转换为XML文件:

select 
    iqr_FirstName AS FIRSTNAME, 
    iqr_LastName AS LASTNAME, 
    iqr_Employer AS EMPLOYER, 
    iqr_Occupation AS OCCUPATION, 
    iqr_Line1 AS ADR1,
    iqr_City AS CITY, iqr_State AS State, 
    iqr_Zip AS ZIP, 
    iqr_CCAmount AS AMOUNT, iqr_CreatedOn AS DATE 
from 
    std7_ImportQueueRecord
for xml path('Individual')

我的嵌套问题是我需要为以下模板分隔节点:

<?xml version="1.0" encoding="UTF-8"?>
<DATABASE name="cc_Sample">
            <INDIVIDUAL>
                        <LASTNAME>last</LASTNAME>
                        <FIRSTNAME>first</FIRSTNAME>
                        <MIDDLE></MIDDLE>
                        <NAMETITLE></NAMETITLE>
                        <NAMESUFFIX></NAMESUFFIX>
                        <BIRTHDATE></BIRTHDATE>
                        <OCCUPATION></OCCUPATION>
                        <EMPLOYER>company</EMPLOYER>
                        <SALUTATION>first</SALUTATION>
                        <GENDER></GENDER>
                        <ADDRESS>
                                    <DESCR>3</DESCR>
                                    <COMPANY></COMPANY>
                                    <ADR1>adr1</ADR1>
                                    <ADR2>adr2</ADR2>
                                    <CITY>city</CITY>
                                    <STATE>st</STATE>
                                    <ZIP>zip</ZIP>
                        </ADDRESS>
                        <PHONENUMBER>
                                    <USES>1</USES>
                                    <PHONE>111-1111</PHONE>
                                    <AREA>111</AREA>
                                    <EXT></EXT>
                        </PHONENUMBER>
                        <PHONENUMBER>
                                    <USES>2</USES>
                                    <PHONE>222-2222</PHONE>
                                    <AREA>222</AREA>
                                    <EXT></EXT>
                        </PHONENUMBER>
                        <PHONENUMBER>
                                    <USES>3</USES>
                                    <PHONE>333-3333</PHONE>
                                    <AREA>333</AREA>
                                    <EXT></EXT>
                        </PHONENUMBER>
                        <EMAILADDRESS>
                                    <EMAIL>test@test.com</EMAIL>
                        </EMAILADDRESS>
                        <CODE>
                                    <ID>2</ID>
                                    <DATE>2005-05-17</DATE>
                                    <MEMO></MEMO>
                        </CODE>
                        <CODE>
                                    <ID>3</ID>
                                    <DATE>2005-05-17</DATE>
                                    <MEMO></MEMO>
                        </CODE>
                        <COMMUNICATION>
                                    <ID>6</ID>
                                    <DATE>2005-05-17</DATE>
                                    <MEMO></MEMO>
                                    <TEXTAREA>details go here</TEXTAREA>
                        </COMMUNICATION>
                       <FINANCIAL>
                                       <TYPE>contribution</TYPE>
                                       <AMOUNT>150</AMOUNT>
                                       <DATE>2005-05-17</DATE>
                                       <CATEGORYID>3</CATEGORYID>
                                       <THANKED>0</THANKED>
                                       <CHECKNUMBER>Credit Card</CHECKNUMBER>
                                       <MEMO></MEMO>
                                       <CARDTYPE>VISA</CARDTYPE>
                                      <CARDNUMBER>4444-4444-4444-4444</CARDNUMBER>
                                       <CVV2>987</CVV2>
                                       <EXPMONTH>02</EXPMONTH>
                                      <EXPYEAR>2012</EXPYEAR>                                                                                                                                            

                      </FINANCIAL>
            </INDIVIDUAL>
</DATABASE>

我没有像我的例子中那样查找如何查询多个节点。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

select 
    iqr_FirstName AS FIRSTNAME, 
    iqr_LastName AS LASTNAME, 
    iqr_Employer AS EMPLOYER, 
    iqr_Occupation AS OCCUPATION, 
    iqr_Line1 AS 'ADDRESS/ADR1',
    iqr_City AS 'ADDRESS/CITY', 
    iqr_State AS 'ADDRESS/State', 
    iqr_Zip AS 'ADDRESS/ZIP', 
    iqr_CCAmount AS AMOUNT, 
    iqr_CreatedOn AS DATE 
from 
    std7_ImportQueueRecord
for xml path('Individual')

将额外的结构(和XML元素)添加到生成的XML