I have the following query:
;WITH XMLNAMESPACES (
'Bob' AS b
)
SELECT 'Test' AS [@b:Test]
FOR XML PATH('root')
However I’d like the name space definition to be dynamically picked up at runtime from a configuration table. I’ve tried the following but they don’t appear to be valid SQL:
-- use a sub query
;WITH XMLNAMESPACES (
(SELECT 'Fred') AS b
)
SELECT 'Test' AS [@b:Test]
FOR XML PATH('root')
-- declare a variable
DECLARE @ns VARCHAR(10) = 'Fred'
;WITH XMLNAMESPACES (
@ns AS b
)
SELECT 'Test' AS [@b:Test]
FOR XML PATH('root')
Is there any way to have the name space definition dynamic?
Thanks,
Tom
You should use dynamic SQL: