我正在创建一个查询,以发送批量电子邮件应用XML,其中包含组装相应电子邮件所需的所有内容。我似乎无法使嵌套元素正常工作。
我正在尝试获取类似
的XML <EmailsToBeSent>
<EmailToBeSent>
<ReferenceId>1</ReferenceId>
<Tags>
<Tag>
<Name>ToAddress</Name>
<Value>blah@blah.com</Value>
</Tag>
<Tag>
<Name>CCAddress</Name>
<Value>yadda@yadda.com</Value>
</Tag>
<Tag>
<Name>FromAddress</Name>
<Value>me@me.com</Value>
</Tag>
</Tags>
<SomethingElse>'asASas'</SomethingElse>
</EmailToBeSent>
</EmailsToBeSent>
到目前为止,我得到的最接近的是:
SELECT
ReferenceId,
(SELECT
'ToAddress' AS "Tag/Name",
ToAddress AS "Tag/Value",
'CCAddress' AS "Tag/Name",
CCAddress AS "Tag/Value",
'FromAddress' AS "Tag/Name",
FromAddress AS "Tag/Value"
FROM
EmailTable AS ET
WHERE
ET.ReferenceId = RT.ReferenceId
FOR XML PATH('Tags'), TYPE),
'asASas' AS SomethingElse
FROM
RefTable AS RT
FOR XML PATH('EmailToBeSent'), ROOT('EmailsToBeSent')
结果是:
<EmailsToBeSent>
<EmailToBeSent>
<ReferenceId>1</ReferenceId>
<Tags>
<Tag>
<Name>ToAddress</Name>
<Value>blah@blah.com</Value>
<Name>CCAddress</Name>
<Value>yadda@yadda.com</Value>
<Name>FromAddress</Name>
<Value>me@me.com</Value>
</Tag>
</Tags>
<SomethingElse>'asASas'</SomethingElse>
</EmailToBeSent>
</EmailsToBeSent>
答案 0 :(得分:2)
只需在群组之间添加分隔符。
SELECT ReferenceId
, (SELECT 'ToAddress' AS "Tag/Name"
, ToAddress AS "Tag/Value"
, null AS "separator"
, 'CCAddress' AS "Tag/Name"
, CCAddress AS "Tag/Value"
, null AS "separator"
, 'FromAddress' AS "Tag/Name"
, FromAddress AS "Tag/Value"
FROM EmailTable AS ET
WHERE ET.ReferenceId = RT.ReferenceId
FOR XML PATH('Tags'),TYPE
)
, 'asASas' AS SomethingElse
FROM RefTable AS RT
FOR XML PATH('EmailToBeSent'), ROOT('EmailsToBeSent')