我在我的选择中有一个声明按预期运行的声明如下:
(CASE WHEN EXISTS (SELECT winnerID
FROM empowermentWinners
WHERE entryID = AA.[entryID])
THEN (SELECT 'Yes' FOR XML PATH ('isWinner'), TYPE, ELEMENTS)
ELSE (SELECT 'No' FOR XML PATH ('isWinner'), TYPE, ELEMENTS)
END AS XML) AS isWinner)
它为我输出XML:
<isWinner>Yes</isWinner>
在此语句中是否有一种方法可以包含select语句中的项目?
例如,如果我想要包含我选择的winnerID;是否有可能像那样返回?
<winnerID>1234</winnerID>
如果需要,我可以将这些更改为内部选择,但如果它能以当前格式返回多条信息,那我就很好奇。
答案 0 :(得分:0)
将子查询移动到from
部分,并使用outer apply
关联它:
select (select case when oa.WinnerId is not null then oa.WinnerId else 0 end as [Winner] for xml path(''), type, elements)
from dbo.MyTable aa
outer apply (
select * from dbo.EmpowermentWinners ew where ew.EntryId = aa.EntryId
) oa