declare @hdoc int, @xmlchar nvarchar(max)
set @xmlchar = '<root> <row _Fld394=" 61640200" /></root>'
exec sp_xml_preparedocument @hdoc out, @xmlchar
select _Fld394
from openxml(@hdoc, '/root/row')
with
(_Fld394 nvarchar(9) '@_Fld394')
exec sp_xml_removedocument @hdoc
//result = '61640200'
//must be = ' 61640200'
如果你看看_Fld394录制了9个字符 - 来自前方空间 提取时,用空格剪掉左边。
如何解决问题?
答案 0 :(得分:3)
declare @hdoc int, @xmlchar nvarchar(max)
set @xmlchar = '<root> <row _Fld394=" asas" /><row _Fld394="" /></root>'
exec sp_xml_preparedocument @hdoc out, @xmlchar
select QuoteName(A) AS A
,QuoteName(B.value('.' ,'varchar(9)')) AS B
,QuoteName(C.value('*[1]/@_Fld394' ,'varchar(9)')) AS C
from openxml(@hdoc, '/root/row')
with (A varchar(9) '@_Fld394', B xml '@_Fld394/text()', C xml '.')
exec sp_xml_removedocument @hdoc
答案 1 :(得分:2)
我认为这是sp_xml_preparedocument的一个已知问题。您可以使用 
编码空格:
set @xmlchar = '<root> <row _Fld394=" 61640200" /></root>'
或(假设SQL 2005或更高版本)使用较新的样式XML数据类型并处理:
declare @xmlchar xml
set @xmlchar = '<root> <row _Fld394=" 61640200" /></root>'
select x.fld.value('@_Fld394','varchar(100)')
from @xmlchar.nodes('//root/row') as x(fld)