2011-10-20 99 views
0

從文檔中獲取max屬性值的最佳xquery方法是什麼?例如sql server 2008中的xquery

<organizations> 
     <organization id="1"/> 
     <organization id="6"/> 
     <organization id="11"/> 
    </organizations> 

我想查詢返回11

感謝

回答

3
declare @xml xml = ' 
<organizations> 
    <organization id="1"/> 
    <organization id="6"/> 
    <organization id="11"/> 
</organizations>' 

select @xml.value('max(/organizations/organization/@id)', 'int') 
1

試試這個:

DECLARE @input XML = '<organizations> 
     <organization id="1"/> 
     <organization id="6"/> 
     <organization id="11"/> 
    </organizations>' 


SELECT 
    MAX(org.value('(@id)[1]', 'int')) 
FROM 
    @input.nodes('/organizations/organization') AS Orgs(Org) 

您需要使用.nodes()得到所有可能的值的列表,並然後將MAX()應用於該整數值列表。

0

您正在查找的XQuery代碼片段是max(/organizations/organization/@id)