【问题标题】:How to resolve maximum length of 128 on a column alias when using FOR XML PATH?使用 FOR XML PATH 时如何解决列别名的最大长度为 128?
【发布时间】:2012-03-29 19:14:57
【问题描述】:

使用以下 SQL,我生成 2 行并将它们包装在单独的肥皂信封中。

declare @XmlDoc xml
;with XMLNAMESPACES 
(
    'http://schemas.xmlsoap.org/soap/envelope/' as soapenv,  
    'http://amsa.com/contract/baserequestcontract/v1.0' as H1,
    'http://MyCompany.org/contract/mrmPerson/v1.0' as N1,
    'http://MyCompany.org/contracts/person' as N2,
    'http://MyCompany.org/contracts/demogTypes' as N3

)

select @XmlDoc = 
(
  select
    top 2
    PersonID as 'soapenv:Header/H1:PersonID',
    IsAuthorizedForUse as 'soapenv:Body/N1:SaveMRMPersonRequest/N1:Person/N2:PersonTier2/N2:AddressArray/N2:Address/N3:CommonDemogInfo/N3:IsAuth'
  from
    dbo.PersonDemogAddress
  for xml 
    path ('soapenv:Envelope'),
    root ('root'),
    type,
    elements xsinil

)
选择@XmlDoc

输出如下所示:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:N3="http://MyCompany.org/contracts/demogTypes" 
  xmlns:N2="http://MyCompany.org/contracts/person" 
  xmlns:N1="http://MyCompany.org/contract/mrmPerson/v1.0" 
  xmlns:H1="http://amsa.com/contract/baserequestcontract/v1.0" 
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">  
<soapenv:Envelope>
<soapenv:Header>
  <H1:PersonID>1</H1:PersonID>
</soapenv:Header>
<soapenv:Body>
  <N1:SaveMRMPersonRequest>
    <N1:Person>
      <N2:PersonTier2>
        <N2:AddressArray>
          <N2:Address>
            <N3:CommonDemogInfo>
              <N3:IsAuth>Y</N3:IsAuth>
            </N3:CommonDemogInfo>
          </N2:Address>
        </N2:AddressArray>
      </N2:PersonTier2>
    </N1:Person>
  </N1:SaveMRMPersonRequest>
</soapenv:Body>
</soapenv:Envelope>  
<soapenv:Envelope>
<soapenv:Header>
  <H1:PersonID>8</H1:PersonID>
</soapenv:Header>
<soapenv:Body>
  <N1:SaveMRMPersonRequest>
    <N1:Person>
      <N2:PersonTier2>
        <N2:AddressArray>
          <N2:Address>
            <N3:CommonDemogInfo>
              <N3:IsAuth>Y</N3:IsAuth>
            </N3:CommonDemogInfo>
          </N2:Address>
        </N2:AddressArray>
      </N2:PersonTier2>
    </N1:Person>
  </N1:SaveMRMPersonRequest>
</soapenv:Body>

但是,对于 xml 列 ,我想使用整个单词 所以我只是将上述查询的 SELECT 部分更改为

select  
  top 2  
    PersonID as 'soapenv:Header/H1:PersonID',  
    IsAuthorizedForUse as 'soapenv:Body/N1:SaveMRMPersonRequest/N1:Person/N2:PersonTier2/N2:AddressArray/N2:Address/N3:CommonDemogInfo/N3:IsAuthorizedForUse

我得到这个错误:

Msg 103, Level 15, State 4, Line 16  
The identifier that starts with 'soapenv:Body/N1:SaveMRMPersonRequest/N1:Person/N2:PersonTier2/N2:AddressArray/N2:Address/N3:CommonDemogInfo/N3:IsAuthorizedForUse' is too long. Maximum length is 128.

有什么办法解决这个问题吗?不幸的是,xml 命名空间、元素名称和层次结构无法更改。谢谢!

【问题讨论】:

    标签: for-xml-path


    【解决方案1】:
    SET QUOTED_IDENTIFIER OFF
    SET ANSI_NULLS ON
    

    把它放在开头它解决了我的问题

    【讨论】:

      【解决方案2】:

      这是一个 sql server 错误。列名不能超过 128 个字符。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-24
        • 2014-08-07
        • 2011-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        • 1970-01-01
        相关资源
        最近更新 更多