我可以找到很多关于如何将某些类型的XML数据导入SQL Server 2005的示例。但我已经获得了以下格式的数据(使用ID而不是标签重复“row”和“cell”命名等:
<?xml version="1.0"?> <rows>
<row id='1'>
<cell id='category'>Simple</cell>
<cell id='query'>summary</cell>
<cell id='clientsfound'>6</cell>
<cell id='eligibleclients'>11</cell>
<cell id='percentage'>55</cell>
<cell id='days'>0</cell>
</row>
<row id='2'>
<cell id='category'>Complex</cell>
<cell id='query'>details</cell>
<cell id='clientsfound'>4</cell>
<cell id='eligibleclients'>6</cell>
<cell id='percentage'>67</cell>
<cell id='days'>5</cell>
</row>
...
</rows>
理想情况下,我想将其加载到表格中,例如:
CREATE TABLE [dbo].[QueryResults](
[UserString] [varchar](50) NULL,
[ImportStamp] [timestamp] NULL,
[RowID] [int] NULL,
[Category] [nchar](10) NULL,
[Query] [nchar](10) NULL,
[ClientsFound] [int] NULL,
[EligibleClients] [int] NULL,
[Percentage] [int] NULL,
[Days] [int] NULL
)
有人可以向我提供一个示例或指向在线教程吗?
答案 0 :(得分:9)
xml应该是“”不在内部,不是吗?
无论如何,您可以本机解析XML数据类型。 由于内存使用的开销,sp_xml_preparedocument非常危险。
DECLARE @foo XML;
SET @foo = N'<?xml version="1.0"?>
<rows>
<row id="1">
<cell id="category">Simple</cell>
<cell id="query">summary</cell>
<cell id="clientsfound">6</cell>
<cell id="eligibleclients">11</cell>
<cell id="percentage">55</cell>
<cell id="days">0</cell>
</row>
<row id="2">
<cell id="category">Complex</cell>
<cell id="query">details</cell>
<cell id="clientsfound">4</cell>
<cell id="eligibleclients">6</cell>
<cell id="percentage">67</cell>
<cell id="days">5</cell>
</row>
</rows>';
SELECT
x.item.value('@id', 'int') AS RowID,
y.item.value('(./cell[@id="category"])[1]', 'nchar(10)') AS category,
y.item.value('(./cell[@id="query"])[1]', 'nchar(10)') AS query,
y.item.value('(./cell[@id="clientsfound"])[1]', 'int') AS clientsfound,
y.item.value('(./cell[@id="eligibleclients"])[1]', 'int') AS eligibleclients,
y.item.value('(./cell[@id="percentage"])[1]', 'int') AS percentage,
y.item.value('(./cell[@id="days"])[1]', 'int') AS days
FROM
@foo.nodes('/rows/row') x(item)
CROSS APPLY
x.item.nodes('.') AS y(item)
答案 1 :(得分:1)
您可以使用OPENXML和XQUERY来完成。
DECLARE @XMLdoc XML
DECLARE @idoc int
SELECT @XMLdoc = '<?xml version="1.0"?>
<rows>
<row id="1">
<cell id="category">Simple</cell>
<cell id="query">summary</cell>
<cell id="clientsfound">6</cell>
<cell id="eligibleclients">11</cell>
<cell id="percentage">55</cell>
<cell id="days">0</cell>
</row>
<row id="2">
<cell id="category">Complex</cell>
<cell id="query">details</cell>
<cell id="clientsfound">4</cell>
<cell id="eligibleclients">6</cell>
<cell id="percentage">67</cell>
<cell id="days">5</cell>
</row>
</rows>'
-- Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @XMLDoc
INSERT INTO QueryResults (RowID,Category,Query,ClientsFound,EligibleClients,Percentage,Days)
SELECT id,
overflow.value('(/row/cell[@id="category"])[1]', 'nchar(10)'),
overflow.value('(/row/cell[@id="query"])[1]', 'nchar(10)'),
overflow.value('(/row/cell[@id="clientsfound"])[1]', 'int'),
overflow.value('(/row/cell[@id="eligibleclients"])[1]', 'int'),
overflow.value('(/row/cell[@id="percentage"])[1]', 'int'),
overflow.value('(/row/cell[@id="days"])[1]', 'int')
FROM OPENXML (@idoc, '/rows/row',10)
WITH (id int '@id',
overflow xml '@mp:xmltext' --the row xml node
)
-- Release resources allocated for the XML document.
EXEC sp_xml_removedocument @idoc
SELECT * FROM QueryResults
结果:
UserString ImportStamp RowID Category Query ClientsFound EligibleClients Percentage Days
----------- ------------------ ------ --------- -------- ------------ --------------- ----------- ----
NULL 0x000000000000C1CA 1 Simple summary 6 11 55 0
NULL 0x000000000000C1CB 2 Complex details 4 6 67 5
我不确定你想要在'UserString'中填充什么,但你可以稍后对其进行排序。
希望这为您的问题提供合适的解决方案。
- 对不起gbn,你对sp_xml_preparedocument的看法可能是正确的。我刚从我们与Microsoft SDC团队合作的项目中的一些类似的存储过程中采用了这种方法,因此认为它是安全的。无论如何,你的方法可能更干净。