我需要在同一个节点路径中有不同的信息,但我希望将这些路径分开,以便更好地理解我添加了我的代码。 以下是我的程序的一部分:
CREATE PROCEDURE [dbo].[CREATE_XML] (@ID_NO NVARCHAR(10))
AS BEGIN
SELECT
1 AS "Case/Client/TelephoneNumbers/TelephoneNumber/NumberTypeId",
P.WORKTEL AS "Case/Client/TelephoneNumbers/TelephoneNumber/Number",
2 AS "Case/Client/TelephoneNumbers/TelephoneNumber/NumberTypeId",
P.MOBILE AS "Case/Client/TelephoneNumbers/TelephoneNumber/Number",
3 AS "Case/Client/TelephoneNumbers/TelephoneNumber/NumberTypeId",
P.HOMETEL AS "Case/Client/TelephoneNumbers/TelephoneNumber/Number"
FROM
DETAILS AS P WHERE ID_NO = @ID_NO
FOR XML path('Telephones')
END
实际输出如下:
<Telephones>
<Case>
<Client>
<TelephoneNumbers>
<TelephoneNumber>
<NumberTypeId>1</NumberTypeId>
<Number>11</Number>
<NumberTypeId>2</NumberTypeId>
<Number>33</Number>
<NumberTypeId>3</NumberTypeId>
<Number></Number>
</TelephoneNumber>
</TelephoneNumbers>
</Client>
</Case>
</Telephones>
但我需要输出:
<Telephones>
<Case>
<Client>
<TelephoneNumbers>
<TelephoneNumber>
<NumberTypeId>1</NumberTypeId>
<Number>11</Number>
</TelephoneNumber>
<TelephoneNumber>
<NumberTypeId>2</NumberTypeId>
<Number>33</Number>
</TelephoneNumber>
<TelephoneNumber>
<NumberTypeId>3</NumberTypeId>
<Number></Number>
</TelephoneNumber>
</TelephoneNumbers>
</Client>
</Case>
</Telephones>
我怎样才能实现这一点?
答案 0 :(得分:0)
经过多次尝试,我找到了解决方案,希望有一天答案会对某人有所帮助。基本上我们需要创建嵌套的select语句,如下所示:
CREATE PROCEDURE [dbo].[CREATE_XML] (@ID_NO NVARCHAR(10))
AS BEGIN
SELECT
(SELECT 1 AS NumberTypeId,
WORKTEL AS Number FROM DETAILS WHERE ID_NO = @ID_NO FOR XML PATH ('TelephoneNumber'), TYPE) AS "Case/Client/TelephoneNumbers",
(SELECT 2 AS NumberTypeId,
MOBILE AS Number FROM DETAILS WHERE ID_NO = @ID_NO FOR XML PATH('TelephoneNumber'), TYPE) AS "Case/Client/TelephoneNumbers",
(SELECT 3 AS NumberTypeId,
HOMETEL AS Number FROM DETAILS WHERE ID_NO = @ID_NO FOR XML PATH('TelephoneNumber'), TYPE) AS "Case/Client/TelephoneNumbers",
FROM DETAILS AS P WHERE ID_NO = @ID_NO FOR XML path('Telephones')
END