Default namespace with FOR XML PATH

好久不见. 提交于 2019-12-06 10:04:18

You can use the "query from hell"

;with CTE_DummyData AS (
    select id=1
)
select 1 as tag,
       0 as parent,
       'http://www.mynamespace.co.uk' as [Parent!1!xmlns],
       id as [Parent!1!ID],
       null as [Child!2!ID]
from CTE_DummyData       
union all
select top 1
       2,
       1,
       null,
       null,
       id
from CTE_DummyData       
for xml explicit

You could use a UDF to generate child nodes. E.g.

ALTER FUNCTION [dbo].[udf_get_child_section] (
    @serviceHeaderId INT
 )
RETURNS XML

BEGIN

    DECLARE @result XML;

    SELECT @result = 
    (
        SELECT 1 AS 'ChildElement'
        FOR XML PATH('Child')
    )

    RETURN @result

END


GO

DECLARE @Ids TABLE
( 
    ID int 
)

INSERT INTO @Ids
SELECT 1 AS ID 
UNION ALL
SELECT 2 AS ID

;WITH XMLNAMESPACES (DEFAULT 'http://www...com/content')
SELECT 
    [dbo].[udf_get_child_section](ID)
FROM 
    @Ids
FOR XML PATH('Parent')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!