【发布时间】:2012-01-04 05:19:20
【问题描述】:
此代码的问题在于每个组中并不总是有一个 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
【问题讨论】:
-
能否提供 XML 示例?
-
我试过了。我不知道你是否能看到我添加的所有内容。
-
这适用于 [1] 的情况,但 [2] 返回空值。
标签: sql-server sql-server-2005 tsql