1

有没有办法添加如下附加属性?

前...

  <Event id="CE1127552523644210147">
    <Title>General Surgery Orange Rotation </Title>
    <Duration>671</Duration>
    <InstructionalMethod>Clinical Rotation</InstructionMethod>
  </Event>

后:

  <Event id="CE1127552523644210147">
    <Title>General Surgery Orange Rotation </Title>
    <Duration>671</Duration>
    <InstructionalMethod Primary='True'>Clinical Rotation</InstructionMethod>
  </Event>

原始查询:

select 
    id as '@id',
    Title,
    Duration,
    InstructionalMethod
from MyTable
for XML PATH ('Event'), ROOT('Events')

基于对 Stack 的搜索,我确实尝试了这个,但没有为元素返回数据。

select 
    id as '@id',
    Title,
    Duration,
    'True' AS 'InstructionalMethod/@Primary'
from mytable
for XML PATH ('Event'), ROOT('Events'), TYPE

结果:

  <Event id="CE1127552523644210147">
    <Title>General Surgery Orange Rotation </Title>
    <Duration>671</Duration>
    <InstructionalMethod Primary="True" />
  </Event>

谢谢你的帮助。

布赖恩

4

1 回答 1

2

你很接近了——但如果你想要这个元素,你也必须在那里有那条线!

尝试这个:

SELECT
    id as '@id',
    Title,
    Duration,
    'True' AS 'InstructionalMethod/@Primary',
    InstructionalMethod   -- add this line to get the actual value as element
FROM 
    dbo.mytable
FOR XML PATH ('Event'), ROOT('Events'), TYPE
于 2012-04-29T16:01:07.147 回答