How to resolve maximum length of 128 on a column alias when using FOR XML PATH?

对着背影说爱祢 提交于 2019-12-12 03:14:13

问题


With the following SQL, I am generating 2 rows and wrapping them in individual soap envelopes.

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

)
select @XmlDoc

the output looks like this:

<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>

However, for the xml column < N3:IsAuth >, I want to use the whole word < N3:IsAuthorizedForUse > so I simply changed SELECT part of above query as

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

And I get this error:

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.

Any way to solve this? The xml namespaces, element names and hierarchy cannot be changed unfortunately. Thanks!


回答1:


SET QUOTED_IDENTIFIER OFF
SET ANSI_NULLS ON

place this at the starting it solved my problem




回答2:


This is a sql server error. There is a limit of 128 characters for column names.



来源:https://stackoverflow.com/questions/9931931/how-to-resolve-maximum-length-of-128-on-a-column-alias-when-using-for-xml-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!