query() Method
XQuery extraction
What changes
- Input
- xml instance and XQuery
- Output or change
- xml fragment
Syntax at a glance
query ('XQuery')
Work example
Return a selected XML fragment when the caller still needs document structure.
Use query for XML output and value when the result should be a typed SQL scalar.
Check your work query returns XML and does not flatten the fragment into a relational string column.
Practice SQL
Open in SQL FlowDECLARE @doc xml = N'<Order><Item code="A1" /></Order>'; SELECT @doc.query('/Order/Item') AS item_fragment;
Open Microsoft Learn for full syntax, permissions, and details for your version.