【发布时间】:2014-05-14 11:23:19
【问题描述】:
我希望能够以这种特定格式写出一个 XML 文件。我一直在阅读bcp 并尝试使用FOR XML,但似乎无法在这里实现我所需要的。我将不胜感激在正确方向上的任何帮助。虽然我知道我可以重构使用这个 XML 输出的代码;我想在这里冒险并坚持手头的任务而不改变任何东西。
用于重新创建临时变量表数据集的 SQL 代码
declare @dataset table(Color nvarchar(10), Number int, Code nvarchar(10))
insert into @dataset
select 'Green', 12345, 'US1'
union
select 'Red', 56789, 'US2'
select * from @dataset
我想通过这个查询生成以下 XML 文档。
<?xml version="1.0" encoding="utf-8" ?>
<test>
<collection>
<Case>
<input>
<attribute name="Color">Green</attribute>
<attribute name="Number">12345</attribute>
<attribute name="Code">US1</attribute>
</input>
</Case>
<Case>
<input>
<attribute name="Color">Red</attribute>
<attribute name="Number">56789</attribute>
<attribute name="Code">US2</attribute>
</input>
</Case>
</collection>
</test>
我会听从各位专家的意见,告诉我这是否太荒谬而无法实现,但我认为这是“可能的”,因为到目前为止我已经有点接近了。
我一直在修改这个并能够写出 XML 文件。
exec master..xp_cmdshell 'bcp "Query Here" queryout "c:\filename.xml" -c -T'
感谢 SO 成员!
【问题讨论】:
标签: sql-server xml tsql bcp