【发布时间】:2011-07-26 19:37:32
【问题描述】:
我在尝试解析存储在表记录中的 xml 时遇到了问题 xml结构如下:
<?xml version="1.0" encoding="utf-16"?>
<WidgetsLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<WidgetsList>
<WidgetConfiguration>
<WidgetId>4</WidgetId>
<DockOrder>0</DockOrder>
<DockZoneId>0</DockZoneId>
<Width />
<Height />
<IsRemovable>true</IsRemovable>
</WidgetConfiguration>
<WidgetConfiguration>
<WidgetId>3</WidgetId>
<DockOrder>0</DockOrder>
<DockZoneId>1</DockZoneId>
<Width />
<Height />
<IsRemovable>true</IsRemovable>
</WidgetConfiguration>
</WidgetsList>
</WidgetsLayout>
并且 xml 以 varchar 形式存储在表记录中。
我需要从 xml 结构中获取包含不同 WidgetId 集合的临时表。
更新:
我确实编写了以下批处理语句来检索 WidgetConfiguration xml 字符串上的集合,因此我可以检索 WidgetId 集合,但我遇到了插入语句的问题:
GO
declare @dashboard_layout table (
id int,
config_xml xml
)
INSERT INTO @dashboard_layout(id)
SELECT
widget_config.value('(WidgetId)[1]', 'int')
FROM
dbo.dashboard_configuration c
CROSS APPLY
CAST(RIGHT(c.configuration_xml_string, LEN(c.configuration_xml_string) - 41), XML).nodes('/WidgetsLayout/WidgetsList/WidgetConfiguration') AS list(widget_config)
select * from @dashboard_layout
在“cast”结果中调用“nodes”时,我在最后一个插入语句行中出现语法错误
提前致谢。
【问题讨论】:
标签: sql-server-2008 xml-parsing