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 に答える