xml数据导入数据库中,错误: 预期的句柄 'eof' 找到 '.'
declare @idoc int
declare @nvd xml
set @nvd ='
<entry name="CVE-2005-0001">
<refs>
<ref source="MISC"></ref>
<ref source="CONECTIVA"></ref>
<ref source="MANDRAKE"></ref>
</refs>
</entry>
'
exec sp_xml_preparedocument @idoc output, @nvd
insert into t
select * from openxml(@idoc,'/nvd/entry/refs/ref')
with(
nvd_name nchar(20) '.../@name',
source varchar(20) './@source'
)
exec sp_xml_removedocument @idoc
运行结果:
消息 6603,级别 16,状态 2,第 16 行
XML 分析错误: 预期的句柄 'eof' 找到 '.'。
..-->.<--/@name
语句已终止。
[最优解释]
declare @idoc int
declare @nvd xml
set @nvd ='
<entry name="CVE-2005-0001">
<refs>
<ref source="MISC"></ref>
<ref source="CONECTIVA"></ref>
<ref source="MANDRAKE"></ref>
</refs>
</entry>
'
exec sp_xml_preparedocument @idoc output, @nvd
--insert into t
select * from openxml(@idoc,'/entry/refs/ref')
with(
nvd_name nchar(20) '//@name[1]',
source varchar(20) '@source'
)
exec sp_xml_removedocument @idoc
set @nvd ='
<nvd>
<entry name="CVE-2005-0001">
<refs>
<ref source="MISC"></ref>
<ref source="CONECTIVA"></ref>
<ref source="MANDRAKE"></ref>
</refs>
</entry>
<entry name="CVE-2005-0002">
<refs>
<ref source="MISC"></ref>
<ref source="CONECTIVA"></ref>
<ref source="MANDRAKE"></ref>
</refs>
</entry>
</nvd>
'
exec sp_xml_preparedocument @idoc output, @nvd
--insert into t
select * from openxml(@idoc,'/nvd/entry/refs/ref')
with(
nvd_name nchar(20) '../../@name',
source varchar(20) '@source'
)
exec sp_xml_removedocument @idoc
[其他解释]
1楼也可以写成这样
declare @idoc int
declare @nvd xml
set @nvd ='
<entry name="CVE-2005-0001">
<refs>
<ref source="MISC"></ref>
<ref source="CONECTIVA"></ref>
<ref source="MANDRAKE"></ref>
</refs>
</entry>
'
exec sp_xml_preparedocument @idoc output, @nvd
--insert into t
select * from openxml(@idoc,'/entry/refs/ref')
with(
nvd_name nchar(20) '../../@name',
source varchar(20) '@source'
)
exec sp_xml_removedocument @idoc