use xmldb
go

drop function OverPricedItems
go

-- overpriced items
create function OverPricedItems(@price money, @theid int)
returns xml
as
begin
 declare @x as xml
 select @x = invoice.query('
    (: XQuery program :)
    declare namespace inv="urn:www-SQLskills-com:invoices"

    for $invitem in //inv:Invoice/inv:LineItems/inv:LineItem
    where $invitem/inv:Price > sql:variable("@price") 
    return
      <OverPriced>
        <Sku> { data($invitem/inv:Sku) } </Sku>
        <Price> { data($invitem/inv:Price) }</Price>
      </OverPriced>
  ') 
 from xmlinvoice
 where id = @TheID
 return @x
end
go

declare @x as xml
set @x = dbo.OverPricedItems(5.00, 1)
print cast(@x as varchar(max))
go

-- SQL Query
select id, invoice.query('
    (: XQuery program :)
    declare namespace inv="urn:www-SQLskills-com:invoices"
    //inv:Invoice/inv:LineItems/inv:LineItem[sql:column("id")]
  ') 
from xmlinvoice
