'WITH XMLNAMESPACES' syntax error in dynamic SQL

I hope some T-SQL XML expert can help me with a solution. Here is the what I try to do:

Thanks a lot!
-----------------

THIS WORKS FINE FOR ME

--==========================================
DECLARE @SQL nvarchar(4000);
DECLARE @entityID nvarchar(4000);
DECLARE @xmlEntityData XML;

SET @SQL = N'WITH XMLNAMESPACES
(
''http://www.w3.org/2001'' AS i,
default ''http://default''
)
SELECT 1234 AS "DataType/Id",
''TEST'' AS "Description",
1111 AS "Id"
FOR XML PATH(''''), ROOT(''FactType'')'

execute sp_executesql @SQL
--==========================================

TRYING THIS BUT HAVE A HARD TIME TO FIGURE OUT A WAY.
IS IT POSSIBLE?

--==========================================
DECLARE @SQL nvarchar(4000);
DECLARE @entityID nvarchar(4000);
DECLARE @xmlEntityData XML;

SET @SQL = N'SELECT @xmlEntityData = (
WITH XMLNAMESPACES
(
''http://www.w3.org/2001'' AS i,
default ''http://default''
)
SELECT 1234 AS "DataType/Id",
''TEST'' AS "Description",
1111 AS "Id"
FOR XML PATH(''''), ROOT(''FactType''))'

execute sp_executesql @SQL, N'@xmlEntityData XML OUTPUT', @xmlEntityData OUTPUT
--==========================================

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ')'.

[1925 byte] By [KL] at [2008-2-7]

SQL Server

Site Classified