【发布时间】:2018-06-07 23:36:31
【问题描述】:
我有以下数据用于 select * from _temp :-
我想在下面生成 xml :-
<xml>
<StoryData>
<UserStoryId>141204</UserStoryId>
<Description>Customer can see the applicable discount on the quote and change in premium.</Description>
<Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
<UserStoryID>141204</UserStoryID>
<VagueWord>and</VagueWord>
<VagueWord>applicable</VagueWord>
</StoryData>
<StoryData>
<UserStoryId>141205</UserStoryId>
<Description>Customer can see the applicable discount on the quote and change in premium.</Description>
<Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
<UserStoryID>141205</UserStoryID>
<VagueWord>and</VagueWord>
<VagueWord>applicable</VagueWord>
</StoryData>
</xml>
我尝试了以下查询:-
select distinct t1.UserStoryId,t1.Description,t1.Summary,t1.UserStoryID,t2.VagueWord
from _temp t1 left join
(
select UserStoryId,VagueWord from _temp
) t2
on t1.UserStoryId=t2.UserStoryId
where t1.UserStoryId in (141204,141205)
FOR XML PATH('StoryData')
,ROOT('xml'),type
正在生成:-
<xml>
<StoryData>
<UserStoryId>141204</UserStoryId>
<Description>Customer can see the applicable discount on the quote and change in premium.</Description>
<Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
<UserStoryID>141204</UserStoryID>
<VagueWord>and</VagueWord>
</StoryData>
<StoryData>
<UserStoryId>141204</UserStoryId>
<Description>Customer can see the applicable discount on the quote and change in premium.</Description>
<Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
<UserStoryID>141204</UserStoryID>
<VagueWord>applicable</VagueWord>
</StoryData>
<StoryData>
<UserStoryId>141205</UserStoryId>
<Description>Customer can see the applicable discount on the quote and change in premium.</Description>
<Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
<UserStoryID>141205</UserStoryID>
<VagueWord>and</VagueWord>
</StoryData>
<StoryData>
<UserStoryId>141205</UserStoryId>
<Description>Customer can see the applicable discount on the quote and change in premium.</Description>
<Summary>Customer can see the applicable discount on the quote and change in premium.</Summary>
<UserStoryID>141205</UserStoryID>
<VagueWord>applicable</VagueWord>
</StoryData>
</xml>
正如我们所见,VagueWord 是多个,StoryData 标记会针对特定的 UserStoryID 重复。
我希望不同的 UserStoryID 和 Vagueword 标记的不同标记在内部重复,如上所示。
我怎样才能做到这一点?
【问题讨论】:
-
以下链接docs.microsoft.com/en-us/sql/relational-databases/xml/… 包含的示例也显示了与您的要求类似的案例
标签: sql sql-server xml sql-server-2008