How can 'XML' data be manipulated in SQL?

Instruction: Describe SQL Server XML data type methods for querying, extracting, expanding and modifying XML, and explain their portability limits.

Context: This question evaluates the candidate's ability to work with XML data types in SQL, reflecting the need to handle various data formats.

Updated

Official Answer

In SQL Server, the xml data type has several complementary methods:

  • query() returns an XML fragment selected with XQuery.
  • value() extracts a scalar into a specified SQL type; the expression must satisfy the method's singleton requirements.
  • exist() returns 1 for a nonempty XQuery result, 0 for an empty result, or NULL for a NULL XML instance. It does not test the Boolean truth of the expression: even false() produces a nonempty result.
  • nodes() exposes selected nodes as a rowset, often used with CROSS APPLY to process repeated elements.
  • modify() changes XML using SQL Server's supported XML DML operations.

For example, an order document with repeated line elements can be decomposed with nodes(), then each line's quantity extracted with value(). Check namespaces, optional elements, multiple matches and invalid conversions against the document contract.

These methods are database-specific. Other products have different XML facilities and syntax; name the engine before offering an executable solution.

Source

Microsoft: SQL Server xml data type methods.

Microsoft: exist() return values and Boolean-expression caveat.

Related Questions