5

Which of the following is a better performing query?

--query a
Select *
UserDefXml.query('/Product/ProductName/text()')
From ProductsDB.dbo.ProductsTable

--query b
Select * 
UserDefXml.value('(/Product/ProductName)[1]','varchar(max)')
From ProductsDB.dbo.ProductsTable

What are the recommended scenarios for each of these? Are there any other ways to accomplish this?

Thank you.

FMFF
  • 1,652
  • 4
  • 32
  • 62
  • 2
    `.query()` returns an XML fragment, while `.value()` returns a single value (int, varchar, etc.) - so you're really comparing apples to ice-cream here - totally different things, totally different scenario - what do you need? XML or a scalar value? – marc_s Oct 01 '15 at 04:55
  • Thank you @marc_s. In my case, they both return a ProductName which is a string. When I click the results returned by `.query()` I don't see any xml tags with it, just the product name. That's why I posted this question. – FMFF Oct 01 '15 at 16:03
  • 2
    That's because of the `/text()` suffix you've added – marc_s Oct 01 '15 at 16:07

0 Answers0