我有下表
EntityID | Data
-----------+-----------
1 | <xml data>
2 | <xml data>
3 | <xml data>
XML DATA如下所示:
<Qualifications>
<Qualification QualificationName="Has Dr's degree" Remark="Yes" />
<Qualification QualificationName="ASP.NET Experience" Remark="1 Year" />
<Qualification QualificationName="Sex" Remark="M" />
</Qualifications>
我希望能够按照特定QualificationName的备注进行订购
SELECT * FROM Table .... ORDER BY&#39; ASP.NET体验&#39;
P.S。 实际上,我可以将XML更改为类似的东西,以使事情更简单
<Qualifications>
<Has Dr's degree>Yes</Has Dr's degree>
<ASP.NET Experience>1 Year</ASP.NET Experience>
<Sex>M</Sex>
</Qualifications>
UPD1:对于用户想要按照“ASP.NET体验”排序的情况。资格预期结果如下:
EntityID | Data
-----------+-----------
3 | <xml data>
1 | <xml data>
2 | <xml data>
因为EntityID 3有备注1年&#39; EntityID 1有2年的评论和#39;和EntityID 2有评论&#3; 3年&#39;用于&#39; ASP.NET Experience&#39;的XML列内部资格
答案 0 :(得分:1)
假设@QualificationName通过此标识您要订购的特定节点,将为您提供备注的值。
declare @xml xml; set @xml = '<Qualifications>
<Qualification QualificationName="Has Dr%quot;s degree" Remark="Yes" />
<Qualification QualificationName="ASP.NET Experience" Remark="1 Year" />
<Qualification QualificationName="Sex" Remark="M" />
</Qualifications>'
declare @Qualification nvarchar(100); set @Qualification = 'ASP.NET Experience'
select @xml.value('(//Qualifications/Qualification[@QualificationName=sql:variable("@Qualification")]/@Remark)[1]', 'varchar(10)')
答案 1 :(得分:1)
我假设您的xml为:
<Qualifications>
<HasDrsdegree>Yes</HasDrsdegree>
<ASPNETExperience>2</ASPNETExperience>
<Sex>M</Sex>
</Qualifications>
我希望此查询可以解决您的问题,此查询会产生一个您可以忽略的额外列,但这样可以一次解决整个表的问题。
Select EntityID,Data,T2.Loc.value('.','int') as 'experience'
from [yourtablename]
cross apply Data.nodes('/Qualifications/ASPNETExperience') as T2(Loc)
order by experience