Skip to content

Latest commit

 

History

History
64 lines (51 loc) · 1.96 KB

File metadata and controls

64 lines (51 loc) · 1.96 KB
title Create views over XML columns
description Learn how to create a view in which the value from an xml type column is retrieved using the value() method of the xml data type.
author MikeRayMSFT
ms.author mikeray
ms.reviewer randolphwest
ms.date 05/05/2022
ms.service sql
ms.subservice xml
ms.topic how-to
ms.custom
ignite-2025
helpviewer_keywords
views [XML in SQL Server]

Create views over XML columns

[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance FabricSQLDB]

You can use an xml type column to create views. The following example creates a view in which the value from an xml type column is retrieved using the value() method of the xml data type.

-- Create the table.
CREATE TABLE T (
    ProductID INT PRIMARY KEY,
    CatalogDescription XML);
GO
-- Insert sample data.
INSERT INTO T VALUES(1,'<ProductDescription ProductID="1" ProductName="SomeName" />');
GO
-- Create view (note the value() method used to retrieve ProductName
-- attribute value from the XML).
CREATE VIEW MyView AS
  SELECT ProductID,
         CatalogDescription.value('(/ProductDescription/@ProductName)[1]', 'varchar(40)') AS PName
  FROM T;
GO

Execute the following query against the view:

SELECT *
FROM   MyView;

This is the result:

ProductID   PName
----------- ------------
1           SomeName

Note the following points about using the xml data type to create views:

  • The xml data type can be created in a materialized view. The materialized view can't be based on an xml data type method. However, it can be cast to an XML schema collection that is different from the xml type column in the base table.

  • The xml data type can't be used in Distributed Partitioned Views.

  • SQL predicates running against the view won't be pushed into the XQuery of the view definition.

  • xml data type methods in a view aren't updatable.