Is it possible to create an indexed view from Xml Data in Sql Server 2008?

后端 未结 2 1523
你的背包
你的背包 2021-01-27 17:03

I see from the 2005 documentation that you cannot create an indexed view from an Xml column.

Is this possible in 2008 or 2008R2? I can\'t find any documentation saying t

2条回答
  •  我在风中等你
    2021-01-27 17:52

    Depending on your need, what you could do is this:

    • create a set of stored functions that extract certain bits of key information from your XML (function receives XML as input, extracts the info using XPath/XQuery, returns a VARCHAR or INT or something value)

      CREATE FUNCTION dbo.SomeFunction(@Input XML)
      RETURNS VARCHAR(20)
      WITH SCHEMABINDING
      AS BEGIN
         ......
      END
      
    • add those key bits to your base table as computed columns that reference those functions, with the PERSISTED keyword:

      ALTER TABLE dbo.YourTable
         ADD ComputedColumns1 AS dbo.SomeFunction(XmlColumn) PERSISTED
      
    • create your view on the table and those computed columns, with schemabinding:

      CREATE VIEW vYourView 
      WITH SCHEMABINDING
      AS  
            SELECT (list of columns)
            FROM dbo.YourTable
      
    • create a unique, clustered index on that view - unless you've violated any of the requirements of the indexed view, this should work just fine:

      CREATE UNIQUE CLUSTERED INDEX CIX_YourView ON dbo.vYourView(.....)
      

    This works fine if you need to extract a small number of key bits of information from an XML column - it's definitely not recommended for lots of XML elements / values.

提交回复
热议问题