我有以下代码开发 2 个 XML 文件,但我希望它们位于一个具有多个组织标签的文件中。
SELECT
LTRIM(RTRIM(c1.cptname)) as "orgHeader/orgCode",
LTRIM(RTRIM(c1.cptname)) as "orgHeader/longName",
LTRIM(RTRIM(c1.cptname)) as "orgHeader/shortName",
'Branch' as "orgRole",
(
SELECT system, extCode from
(
Select 'a' AS 'system', LTRIM(RTRIM(c1.cptname)) AS
'extCode'
UNION ALL
Select 'b' AS 'system', LTRIM(RTRIM(c1.cptname)) AS
'extCode'
UNION ALL
Select 'Manual' AS 'system', LTRIM(RTRIM(c1.cptname)) AS
'extCode'
) a
FOR XML PATH('externalCode'), TYPE, ELEMENTS,Root('externalCodes')
)
from cpt c1
where cptleagname like '%abc%'
and cptcod IN (select cptcod from pf_map
where pf IN ('abc','abc-jp')
and stat = 'a')
FOR XML PATH('organisation'), root ('collection')
SELECT
LTRIM(RTRIM(c1.cptname))+'.' as "orgHeader/orgCode",
LTRIM(RTRIM(c1.cptname))+'.' as "orgHeader/longName",
LTRIM(RTRIM(c1.cptname))+'.' as "orgHeader/shortName",
(
SELECT orgRole from
(
Select 'Coll' AS 'orgRole'
UNION ALL
Select 'Lega' AS 'orgRole'
) a
FOR XML PATH(''), TYPE, ELEMENTS
)
from cpt c1
where cptleagname like '%abc%'
and cptcod IN (select cptcod from pf_map
where pf IN ('abc','abc-jp')
and stat = 'a')
FOR XML PATH('organisation'), root ('collection')
当我运行它时,它会生成 2 个 XML 文件作为输出,如下所示。
<collection>
<organisation>
<orgHeader>
<orgCode>abc</orgCode>
<longName>abc</longName>
<shortName>abc</shortName>
</orgHeader>
<orgRole>Branch</orgRole>
<externalCodes>
<externalCode>
<system>a</system>
<extCode>abc</extCode>
</externalCode>
<externalCode>
<system>b</system>
<extCode>abc</extCode>
</externalCode>
<externalCode>
<system>Manual</system>
<extCode>abc</extCode>
</externalCode>
</externalCodes>
</organisation>
和
<collection>
<organisation>
<orgHeader>
<orgCode>abc.</orgCode>
<longName>abc.</longName>
<shortName>abc.</shortName>
</orgHeader>
<orgRole>Coll</orgRole>
<orgRole>Leg</orgRole>
</organisation>
</collection>
虽然我想将它添加到单个文件下以将结果作为 Collection 作为 root 并将这些文件合并到组织标记中,如下所示。
<collection>
<organisation>
<orgHeader>
<orgCode>abc</orgCode>
<longName>abc</longName>
<shortName>abc</shortName>
</orgHeader>
<orgRole>Branch</orgRole>
<externalCodes>
<externalCode>
<system>a</system>
<extCode>abc</extCode>
</externalCode>
<externalCode>
<system>b</system>
<extCode>abc</extCode>
</externalCode>
<externalCode>
<system>Manual</system>
<extCode>abc</extCode>
</externalCode>
</externalCodes>
</organisation>
<organisation>
<orgHeader>
<orgCode>abc.</orgCode>
<longName>abc.</longName>
<shortName>abc.</shortName>
</orgHeader>
<orgRole>Coll</orgRole>
<orgRole>Leg</orgRole>
</organisation>
</collection>
如果需要任何其他信息,请在下方评论。谢谢。
接近你想要的输出的东西:
输出:
您说
orgRole
应该删除父标签。我们接近最终解决方案,但还没有。我有一个想法FOR XML EXPLICIT
它的输出:
一个小评论:
externalCodes/externalCode
当我们在对应列中有空值时,仍然会出现(带有空值)。You can use two variables : one to store the first part of the xml and one to store the second part; then add as prefix
<collection>
and a suffix `'output of this:
dbfiddle here
LATER ADDED
to export with bcp:
[test].[dbo].[test_xml]
it is a stored procedure where you will place your code inside.