如何从sql脚本中获取xsd元素格式结果

如何从sql脚本中获取xsd元素格式结果,sql,tsql,sql-server-2008-r2,sql-scripts,Sql,Tsql,Sql Server 2008 R2,Sql Scripts,我需要创建xml文件来将大量数据从一个数据库迁移到另一个数据库 如何从ms sql脚本查询中获得以下xsd格式的结果 如果你有任何想法,请分享 xml文件格式如下: <Batch> <retail:customer xmlns:core="http://www.bactor.com/core" xmlns:retail=""http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-

我需要创建xml文件来将大量数据从一个数据库迁移到另一个数据库

如何从ms sql脚本查询中获得以下xsd格式的结果

如果你有任何想法,请分享

xml文件格式如下:

<Batch>
    <retail:customer xmlns:core="http://www.bactor.com/core" xmlns:retail=""http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <retail:addresses>
            <retail:address>
                <retail:country>GB</retail:country>
                <retail:countryCodeId></retail:countryCodeId>
                <retail:isPreferred>true</retail:isPreferred>
                <retail:isActive>true</retail:isActive>
                <retail:typeId>PERSONAL_ADDRESS</retail:typeId>
                <retail:postCode>2344</retail:postCode>
                <retail:street1>1234214</retail:street1>
                <retail:isTemplate>false</retail:isTemplate>
                <retail:referenceId></retail:referenceId>
                <retail:addressReferenceId>0727-:83f5bd9f331:e8e438a1:fa34668911272008</retail:addressReferenceId>
            </retail:address>
        </retail:addresses>
        <retail:contactMethod></retail:contactMethod>
        <retail:contactable>false</retail:contactable>
        <retail:maritalStatus></retail:maritalStatus>
        <retail:nationality></retail:nationality>
        <retail:noChildren>0</retail:noChildren>
        <retail:customerNumber>1</retail:customerNumber>
        <retail:emailAddresses>
            <retail:emailAddress>alice@wonderland.hearts</retail:emailAddress>
            <retail:preferred>true</retail:preferred>
            <retail:restrictedReason></retail:restrictedReason>
            <retail:status></retail:status>
            <retail:typeId>PERSONAL_EMAIL</retail:typeId>
            <retail:referenceId></retail:referenceId>
            <retail:active>false</retail:active>
        </retail:emailAddresses>
        <retail:phoneNumbers>
            <retail:countryCode></retail:countryCode>
            <retail:number>11222445566</retail:number>
            <retail:preferred>true</retail:preferred>
            <retail:restrictedReason></retail:restrictedReason>
            <retail:status></retail:status>
            <retail:typeId>LANDLINE</retail:typeId>
            <retail:referenceId></retail:referenceId>
            <retail:active>true</retail:active>
            <retail:phoneNumberReferenceId>e437-:83f5bd9f331:e8e438a1:fa34668911272008</retail:phoneNumberReferenceId>
        </retail:phoneNumbers>
        <retail:customerName>
            <retail:surname>AppleGate</retail:surname>
            <retail:forename>Alice</retail:forename>
            <retail:title>Mrs</retail:title>
            <retail:sex>FEMALE</retail:sex>
            <retail:dateOfBirth>2012-09-12T00:00:00+01:00</retail:dateOfBirth>
        </retail:customerName>
        <retail:businessContactMethod></retail:businessContactMethod>
        <retail:preferredContactTime></retail:preferredContactTime>
        <retail:allowInternalMarketing>true</retail:allowInternalMarketing>
        <retail:allowExternalMarketing>true</retail:allowExternalMarketing>
        <retail:employeeKey></retail:employeeKey>
        <retail:customerType>RETAIL</retail:customerType>
        <retail:organisation></retail:organisation>
        <retail:taxIdentification></retail:taxIdentification>
        <retail:companyNumber></retail:companyNumber>
        <retail:createdBy></retail:createdBy>
        <retail:createdAt></retail:createdAt>
        <retail:status>New</retail:status>
        <retail:source></retail:source>
    </retail:customer>
    <retail:customer xmlns:core="http://www.enactor.com/core" xmlns:retail="http://www.enactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <retail:addresses>
            <retail:address>
                <retail:country>GB</retail:country>
                <retail:countryCodeId></retail:countryCodeId>
                <retail:isPreferred>true</retail:isPreferred>
                <retail:isActive>true</retail:isActive>
                <retail:typeId>PERSONAL_ADDRESS</retail:typeId>
                <retail:postCode>2344</retail:postCode>
                <retail:street1>1234214</retail:street1>
                <retail:isTemplate>false</retail:isTemplate>
                <retail:referenceId></retail:referenceId>
                <retail:addressReferenceId>0727-:83f5bd9f331:e8e438a1:fa34668911272008</retail:addressReferenceId>
            </retail:address>
        </retail:addresses>
        <retail:contactMethod></retail:contactMethod>
        <retail:contactable>false</retail:contactable>
        <retail:maritalStatus></retail:maritalStatus>
        <retail:nationality></retail:nationality>
        <retail:noChildren>0</retail:noChildren>
        <retail:customerNumber>1</retail:customerNumber>
        <retail:emailAddresses>
            <retail:emailAddress>alice@wonderland.hearts</retail:emailAddress>
            <retail:preferred>true</retail:preferred>
            <retail:restrictedReason></retail:restrictedReason>
            <retail:status></retail:status>
            <retail:typeId>PERSONAL_EMAIL</retail:typeId>
            <retail:referenceId></retail:referenceId>
            <retail:active>false</retail:active>
        </retail:emailAddresses>
        <retail:phoneNumbers>
            <retail:countryCode></retail:countryCode>
            <retail:number>11222445566</retail:number>
            <retail:preferred>true</retail:preferred>
            <retail:restrictedReason></retail:restrictedReason>
            <retail:status></retail:status>
            <retail:typeId>LANDLINE</retail:typeId>
            <retail:referenceId></retail:referenceId>
            <retail:active>true</retail:active>
            <retail:phoneNumberReferenceId>e437-:83f5bd9f331:e8e438a1:fa34668911272008</retail:phoneNumberReferenceId>
        </retail:phoneNumbers>
        <retail:customerName>
            <retail:surname>AppleGate</retail:surname>
            <retail:forename>Alice</retail:forename>
            <retail:title>Mrs</retail:title>
            <retail:sex>FEMALE</retail:sex>
            <retail:dateOfBirth>2012-09-12T00:00:00+01:00</retail:dateOfBirth>
        </retail:customerName>
        <retail:businessContactMethod></retail:businessContactMethod>
        <retail:preferredContactTime></retail:preferredContactTime>
        <retail:allowInternalMarketing>true</retail:allowInternalMarketing>
        <retail:allowExternalMarketing>true</retail:allowExternalMarketing>
        <retail:employeeKey></retail:employeeKey>
        <retail:customerType>RETAIL</retail:customerType>
        <retail:organisation></retail:organisation>
        <retail:taxIdentification></retail:taxIdentification>
        <retail:companyNumber></retail:companyNumber>
        <retail:createdBy></retail:createdBy>
        <retail:createdAt></retail:createdAt>
        <retail:status>New</retail:status>
        <retail:source></retail:source>
    </retail:customer>
    </Batch>
试试这个:

DECLARE @xml XML

;WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS xsi, 'http://www.bactor.com/retail' AS retail, 'http://www.bactor.com/core' AS core)
SELECT @xml = (SELECT 
[retail:Customer].id AS [retail:id],
[retail:Customer].contactMethod AS [retail:contactMethod],
[retail:Customer].contactable AS [retail:contactable],
[retail:address].emailAddress AS [retail:emailAddress],
[retail:address].typeId AS [retail:typeId]

FROM dbo.Customers AS [retail:Customer]
JOIN dbo.emailAddresses AS [retail:address] ON [retail:Customer].id = [retail:address].customerID
FOR XML AUTO, ELEMENTS)

SET @xml = '<Batch>' + CAST(@xml AS NVARCHAR(max)) + '</Batch>'
SELECT @xml
输出:

<Batch>
  <retail:Customer xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <retail:id>1</retail:id>
    <retail:contactMethod>1</retail:contactMethod>
    <retail:contactable>1</retail:contactable>
    <retail:address>
      <retail:emailAddress>some@some.some</retail:emailAddress>
      <retail:typeId>1</retail:typeId>
    </retail:address>
    <retail:address>
      <retail:emailAddress>some@some.some</retail:emailAddress>
      <retail:typeId>2</retail:typeId>
    </retail:address>
  </retail:Customer>
  <retail:Customer xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <retail:id>2</retail:id>
    <retail:contactMethod>1</retail:contactMethod>
    <retail:contactable>0</retail:contactable>
    <retail:address>
      <retail:emailAddress>some@some.some</retail:emailAddress>
      <retail:typeId>3</retail:typeId>
    </retail:address>
  </retail:Customer>
</Batch>
编辑:

你可以这样做:

DECLARE @xml XML

;WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS xsi, 'http://www.bactor.com/retail' AS retail, 'http://www.bactor.com/core' AS core)
SELECT @xml = (SELECT 
[retail:Customer].id AS [retail:id],
[retail:Customer].contactMethod AS [retail:contactMethod],
[retail:Customer].contactable AS [retail:contactable],

(SELECT 
[retail:address].emailAddress ,
[retail:address].typeId
FROM dbo.emailAddresses AS [retail:address] WHERE [retail:Customer].id = [retail:address].customerID
FOR XML AUTO, TYPE, ELEMENTS, ROOT('retail:Addresses')
)
FROM dbo.Customers AS [retail:Customer]
FOR XML AUTO, ELEMENTS)

SET @xml = '<Batch>' + CAST(@xml AS NVARCHAR(max)) + '</Batch>'
SELECT @xml
但名称空间已添加到所有子项:

<Batch>
  <retail:Customer xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <retail:id>1</retail:id>
    <retail:contactMethod>1</retail:contactMethod>
    <retail:contactable>1</retail:contactable>
    <retail:Addresses xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <retail:address>
        <emailAddress>some@some.some</emailAddress>
        <typeId>1</typeId>
      </retail:address>
      <retail:address>
        <emailAddress>some@some.some</emailAddress>
        <typeId>2</typeId>
      </retail:address>
    </retail:Addresses>
  </retail:Customer>
  <retail:Customer xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <retail:id>2</retail:id>
    <retail:contactMethod>1</retail:contactMethod>
    <retail:contactable>0</retail:contactable>
    <retail:Addresses xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <retail:address>
        <emailAddress>some@some.some</emailAddress>
        <typeId>3</typeId>
      </retail:address>
    </retail:Addresses>
  </retail:Customer>
</Batch>
这是一个已知问题,您可以在此处查看详细信息:


额外的名称空间不会造成任何伤害,但会增加生成的xml文档的大小。

不过这非常接近。我只是把名字放进去了,而不是放在上面。它真的很好用。还有一件事-。我需要像这样生产。
<Batch>
  <retail:Customer xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <retail:id>1</retail:id>
    <retail:contactMethod>1</retail:contactMethod>
    <retail:contactable>1</retail:contactable>
    <retail:Addresses xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <retail:address>
        <emailAddress>some@some.some</emailAddress>
        <typeId>1</typeId>
      </retail:address>
      <retail:address>
        <emailAddress>some@some.some</emailAddress>
        <typeId>2</typeId>
      </retail:address>
    </retail:Addresses>
  </retail:Customer>
  <retail:Customer xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <retail:id>2</retail:id>
    <retail:contactMethod>1</retail:contactMethod>
    <retail:contactable>0</retail:contactable>
    <retail:Addresses xmlns:core="http://www.bactor.com/core" xmlns:retail="http://www.bactor.com/retail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <retail:address>
        <emailAddress>some@some.some</emailAddress>
        <typeId>3</typeId>
      </retail:address>
    </retail:Addresses>
  </retail:Customer>
</Batch>