这是另一个'for xml'问题,但我不确定这是否可以在没有显式模式的情况下完成。如果它不能,那么我将不得不忍受它。
以下是我目前的选择陈述:
SELECT
[stream_id] as '@stream_id',
[path_locator] as '@path_locator',
[parent_path_locator] as '@parent_path_locatr',
[file_type] as '@file_type',
[cached_file_size] as '@cached_file_size',
[creation_time] as '@creation_time',
[last_write_time] as '@last_write_time',
[last_access_time] as '@last_access_time',
[is_directory] as '@is_directory',
[is_offline] as '@is_offline',
[is_hidden] as '@is_hidden',
[is_readonly] as '@is_readonly',
[is_archive] as '@is_archive',
[is_system] as '@is_system',
[is_temporary] as '@is_temporary',
[name] as '/name',
dbo.udf_GetChildren(path_locator)
FROM @Merged
WHERE path_locator.GetLevel() = 1
FOR XML PATH'file'), ROOT('files'), TYPE
这将输出以下xml:
<files>
<file stream_id="" etc...>
<name>NAME</name>
</file>
</files>
这还不错,但我真正喜欢的是将 name 元素的值作为 file 元素的值。这是一个如此简单的任务,我认为它可以在没有显式模式的情况下完成,但我经常对这些事情做错了。
我尝试使用'/'标记,但这似乎没有我想要的效果(例如Update XML node (in an XML column) in SQL Server 2005 with another column value in the same row)。
修改 所需的xml就是这样:
<files>
<file stream_id="" etc...>NAME</file>
</files>
非常感谢你的帮助。
答案 0 :(得分:3)
使用name as '*'
答案 1 :(得分:2)
我认为您必须从字段中删除列名称 - 我能够将空白空间连接到末尾。这是一个浓缩版本,您应该可以使用上面的示例:
SELECT name + ''
FROM TestXML
FOR XML PATH('file')
这是SQL Fiddle。
关于它的好文章:
http://msdn.microsoft.com/en-us/library/bb510469.aspx
以上是您的上述查询:
SELECT
[stream_id] as '@stream_id',
[path_locator] as '@path_locator',
[parent_path_locator] as '@parent_path_locatr',
[file_type] as '@file_type',
[cached_file_size] as '@cached_file_size',
[creation_time] as '@creation_time',
[last_write_time] as '@last_write_time',
[last_access_time] as '@last_access_time',
[is_directory] as '@is_directory',
[is_offline] as '@is_offline',
[is_hidden] as '@is_hidden',
[is_readonly] as '@is_readonly',
[is_archive] as '@is_archive',
[is_system] as '@is_system',
[is_temporary] as '@is_temporary',
[name] + '',
dbo.udf_GetChildren(path_locator)
FROM @Merged
WHERE path_locator.GetLevel() = 1
FOR XML PATH'file'), ROOT('files'), TYPE
祝你好运。
答案 2 :(得分:1)
我将file
节点移到了每一列:
SELECT [stream_id] AS 'file/@stream_id'
, [is_system] AS 'file/@is_system'
, [is_temporary] AS 'file/@is_temporary'
...
, [name] AS 'file'
FROM Merged
FOR XML PATH('files'), TYPE;