我有一个表,其中的nvarchar(max)
列可能包含已知结构的 xml。我想解析它以执行一些聚合,因此需要过滤掉“坏”条目。这是测试用例:
create table TestTable ([Message] nvarchar(max))
insert into TestTable ([Message]) values
('<root m="1"/>'),
('<root m="7"/>'),
('<rooo')
go
set quoted_identifier on
go
create view TestView as
select data.value('(/root/@m)[1]', 'int') as MyValue
from (
select cast([Message] as xml) as data
from (
select [Message] from dbo.TestTable where [Message] like '<root%>'
) as T1
) as T2
where data.exist('/root') = 1
go
select * from TestView
这会产生:
消息 9400,级别 16,状态 1,第 1 行 XML 解析:第 1 行,字符 5,输入意外结束
我不明白为什么,因为如果我运行嵌套查询:
select cast([Message] as xml) as data
from (
select [Message] from dbo.TestTable where [Message] like '<root%>'
) as T1
它完美地返回 2 个有效行。为什么??
ps Microsoft SQL Server 2008 (SP3) - 10.0.5500.0 (X64) Sep 21 2011 22:45:45 版权所有 (c) 1988-2008 Microsoft Corporation Express Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1 )