获取在当前 xml 节点内显示的值

Get value to show inside current xml node

这就是我遇到的问题,我希望这是有道理的。我编写了一个 sql 脚本,使用 for xml 路径 .

将表中的数据导出到一个 xml 文件中

这是一个示例(您应该能够复制、粘贴和 运行 下面的脚本):

    create table #temptable(
    mykey nvarchar(200),
    myarea nvarchar(200),
    mytype nvarchar(200),
    myvalue nvarchar(max)
    )

    insert into #temptable values ('6385465665245', 'area1', 'type1', 'This area should be inside the keyareatypes node and NOT in the value node.')
    insert into #temptable values ('6632525685488', 'area2', 'type2', 'This area should be inside the keyareatypes node and NOT in the value node.')

    select
    tmp.mykey as '@key',
    tmp.myarea as '@area',
    tmp.mytype as '@type',
    tmp.myvalue as 'value'

    from
    #temptable tmp

    for xml path('keyareatypes'), type

    IF OBJECT_ID('tempdb..#temptable') IS NOT NULL
        DROP TABLE #temptable

如果我 运行 上述查询,我​​得到以下信息:

<keyareatypes key="6385465665245" area="area1" type="type1">
    <value>This area should be inside the keyareatypes node and NOT in the value node.</value>
</keyareatypes>
<keyareatypes key="6632525685488" area="area2" type="type2">
    <value>This area should be inside the keyareatypes node and NOT in the value node.</value>
</keyareatypes>

如您所见,在 <keyareatypes> 节点中是另一个名为 <value> 的节点。我不想要那里的 <value> 节点。

任何帮助将不胜感激。

谢谢。

我认为这给出了正确答案:

create table #temptable(
mykey nvarchar(200),
myarea nvarchar(200),
mytype nvarchar(200),
myvalue nvarchar(max)
)

insert into #temptable values ('6385465665245', 'area1', 'type1', 'This area should be inside the keyareatypes node and NOT in the value node.')
insert into #temptable values ('6632525685488', 'area2', 'type2', 'This area should be inside the keyareatypes node and NOT in the value node.')

select
mykey as 'keyareatypes/@key',
myarea as 'keyareatypes/@area',
mytype as 'keyareatypes/@type',
myvalue as keyareatypes
from
#temptable as keyareatypes
for xml path(''),TYPE

IF OBJECT_ID('tempdb..#temptable') IS NOT NULL
    DROP TABLE #temptable

这个returns

<keyareatypes key="6385465665245" area="area1" type="type1">
  This area should be inside the keyareatypes node and NOT in the value node.
</keyareatypes>
<keyareatypes key="6632525685488" area="area2" type="type2">
  This area should be inside the keyareatypes node and NOT in the value node.
</keyareatypes>