此代码的问题在于每个组中并不总是有CEEBCode。它找到的第一个ceebcode实际上在第二组中,并与第一组中的其他元素匹配。
当ceeb代码不存在时,是否仍然将组保持在一起?
select
UIN,
docXML.value('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool/CEEBCode/text())[1]'
,'varchar(6)'
) as ceeb1,
docxml.value
('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool/IsGrad/text())[1]'
,'varchar(5)'
) as IsGradAns1,
docxml.value
('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool/Country/text())[1]'
,'varchar(25)'
) as Country1,
docXML.value('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool/CEEBCode/text())[2]'
,'varchar(6)'
) as ceeb2,
docxml.value
('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool/IsGrad/text())[2]'
,'varchar(5)'
) as IsGradAns2,
docxml.value
('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool/Country/text())[2]'
,'varchar(25)'
) as Country2
from tblDocument
where docxml is not null
示例XML:
<SelfReportedTranscript>
<HighSchools>
<SelfReportedHighSchool>
<Name>Ramay Jr High School</Name>
<City>Fayetteville</City>
<Country>United States of America</Country>
<StateProvince>Arkansas</StateProvince>
<IsGrad>false</IsGrad>
<HighSchoolType>Domestic</HighSchoolType>
</SelfReportedHighSchool>
<SelfReportedHighSchool>
<Name>Fayetteville Sr High Sch</Name>
<CEEBCode>040770</CEEBCode>
<City>Fayetteville</City>
<Country>US</Country>
<StateProvince>AR</StateProvince>
<IsGrad>true</IsGrad>
<HighSchoolType>Domestic</HighSchoolType>
</SelfReportedHighSchool>
</HighSchools>
</SelfReportedTranscript>
这适用于SQL Server 2005 SP3
答案 0 :(得分:0)
试试这个。已将[1]
和[2]
添加到SelfReportedHighSchool
代码中,因为您可以从中获取“行”。还需要将最终[2]
更改为[1]
。
select
UIN,
docXML.value('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool[1]/CEEBCode/text())[1]'
,'varchar(6)'
) as ceeb1,
docxml.value
('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool[1]/IsGrad/text())[1]'
,'varchar(5)'
) as IsGradAns1,
docxml.value
('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool[1]/Country/text())[1]'
,'varchar(25)'
) as Country1,
docXML.value('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool[2]/CEEBCode/text())[1]'
,'varchar(6)'
) as ceeb2,
docxml.value
('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool[2]/IsGrad/text())[1]'
,'varchar(5)'
) as IsGradAns2,
docxml.value
('
(/SelfReportedTranscript/HighSchools/SelfReportedHighSchool[2]/Country/text())[1]'
,'varchar(25)'
) as Country2
from tblDocument
where docxml is not null