2013-07-11 64 views
0

我正在使用SQL Server 2005/2008 xml/xquery。我有以下xml,並且我想返回<all>中的所有元素,但<except>中的元素除外。Xquery(SQL Server 2005/2008)except/outer join?

declare @x xml = ' 
<except> 
    <x>1</x> 
    <x>4</x> 
</except> 
<all> 
    <x>1</x> 
    <x>2</x> 
    <x>3</x> 
    <x>4</x> 
</all>' 
select @x.query('for $x in /all return $x') 
-- How to except the elements in <except>? 

預期結果:<x>2</x><x>3</x>

回答

1

我一直在嘗試並找到一種解決方案。可以嗎?

select @x.query('let $e := /all/x[not(. = /except/x)] 
return $e') 
+1

如果你的節點值是一個簡單的類型,那就太好了。如果它是一個節點而不是一個整數,那麼你需要做的不僅僅是一般性的比較,因爲例如' = '評估爲'true',這可能起初是令人驚訝的。 – dirkk

+0

你不需要'let/return' - 只要'/ all/x [not(。=/except/x)]''。 –