SQL Flow Field Guide

WITH XMLNAMESPACES

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

declares XML namespaces for XQuery/FOR XML in one statement

What changes

Input
namespace URI declarations
Output or change
prefix bindings for the statement

Syntax at a glance

WITH XMLNAMESPACES ( <XML namespace declaration item> [ { , <XML namespace declaration item> }...] )

Work example

Bind short prefixes to namespaces before querying XML that uses namespace-qualified names.

Declare the same namespace URI used by the document and use its prefix in each XQuery path.

Check your work An element in a default XML namespace does not match an unqualified path.

Practice SQL

Open in SQL Flow
DECLARE @doc xml = N'<i:Order xmlns:i="urn:inventory" id="7" />'; WITH XMLNAMESPACES ('urn:inventory' AS i) SELECT @doc.value('(/i:Order/@id)[1]', 'int') AS order_id;

Open Microsoft Learn for full syntax, permissions, and details for your version.