SQL Server中的选择性XML索引

时间:2014-04-17 19:35:19

标签: sql sql-server xml indexing

我有一个包含数千行的表,在名为OldValue的列中包含以下数据:

<attendance xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CalendarId>100</CalendarId>
  <PeriodId>1019</PeriodId>
  <PersonId>10457</PersonId>
  <Date>2012-01-04 00:00:00</Date>
  <ExcuseId>884</ExcuseId>
</attendance>

我正在尝试设置一个选择性的xml索引。以下是我到目前为止所做的事情:

创建选择性XML索引:

CREATE SELECTIVE XML INDEX sxi_Test ON [dbo].[mytable](OldValue)
WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as yming)
FOR
(    
pathWH2 = '/yming:Attendance/PersonId' AS XQUERY 'xs:string' SINGLETON
);
GO

查询索引总是返回0行:

WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as yming)
SELECT COUNT(*)
FROM mytable
WHERE OldValue.exist(N'(/yming:Attendance/yming:PersonId[.=10457])') = 1
GO

有什么建议吗?谢谢。

1 个答案:

答案 0 :(得分:0)

我经常在SQL中遇到涉及XML命名空间的任何问题的解决方案,但这似乎对我有用:

DECLARE @x XML = '<attendance xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <CalendarId>100</CalendarId> <PeriodId>1019</PeriodId> <PersonId>10457</PersonId> <Date>2012-01-04 00:00:00</Date> <ExcuseId>884</ExcuseId> </attendance>';

WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as yming)
SELECT @x.exist(N'(/attendance/PersonId[.=10457])')
GO