18
XML in SQL Server 2005

XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

Embed Size (px)

Citation preview

Page 1: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

XML in SQL Server 2005

Page 2: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

Overview

• XML is a key part of any modern data environment

• It can be used to transmit data in a platform, application neutral form

• It can be used to store document type data that is not easily described in normal table structures

• SQL Server 2005 has many advanced xml features

Page 3: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

Relational to XML

• SQL Server provides four methods to output relational data as XML– FOR XML RAW– FOR XML AUTO– FOR XML PATH– FOR XML EXPLICIT

Page 4: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

For XML Raw

• Xml Raw is the easiest to use. With no other arguments it outputs an xml fragment with each column represented as an attribute, value pair.

SELECT LastName, Firstname, HomePhone

FROM Employees

FOR XML RAW

Page 5: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

XML RAW Results

<row LastName="Davolio" Firstname="Nancy" HomePhone="(206) 555-9857" />

<row LastName="Fuller" Firstname="Andrew" HomePhone="(206) 555-9482" />

<row LastName="Leverling" Firstname="Janet" HomePhone="(206) 555-3412" />

<row LastName="Peacock" Firstname="Margaret" HomePhone="(206) 555-8122" />

<row LastName="Buchanan" Firstname="Steven" HomePhone="(71) 555-4848" />

Page 6: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

Elements and Root

• To change the output you can use the “ELEMENTS” keyword to have the columns output as elements instead of attributes.

• You can also add a root element with the keyword “ROOT” so that the result is a well-formed XML file

Page 7: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

EXAMPLE with ELEMENTS and ROOT

SELECT LastName, Firstname, HomePhone

FROM EmployeesFOR XML RAW('Employees'),

ELEMENTS, ROOT('EmployeeList')

Page 8: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

Limits of XML RAW

• All columns must be formatted in the same way—all attributes or all elements

• Only a one level hierarchy– no nested levels

Page 9: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

For XML Auto

• XML Auto allows you to create nested structures

• SELECT CategoryName, • ProductName, • QuantityperUnit, • unitprice• FROM categories• INNER JOIN Products• ON Categories.CategoryID=Products.CategoryID• FOR XML AUTO, ELEMENTS,

ROOT('ProductCategories')

Page 10: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

<ProductCategories> <categories> <CategoryName>Beverages</CategoryName> <Products> <ProductName>Chai</ProductName> <QuantityperUnit>10 boxes x 20 bags</QuantityperUnit> <unitprice>18.0000</unitprice> </Products> <Products> <ProductName>Chang</ProductName> <QuantityperUnit>24 - 12 oz bottles</QuantityperUnit> <unitprice>19.0000</unitprice> </Products> </categories>

Page 11: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

For XML PATH

• XML Path provides full control over the XML and is easier than XML EXPLICIT

SELECT CategoryName 'Category/CategoryName',ProductName 'Category/Product/ProductName',QuantityPerUnit 'Category/Product/QuantityPerUnit',UnitPrice 'Category/Product/price/data()'FROM categoriesINNER JOIN ProductsON Categories.CategoryID=Products.CategoryIDOrder By CategoryNameFOR XML PATH, ROOT('ProductCategories')

Page 12: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

For XML Explicit

• Explict provides even more control over the xml but is complex to implement

Page 13: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

Storing XML in the Database

• Xml can be stored in the database as either text or

• Starting with SQL Server 2005 as a native XML data type

Page 14: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

As Text

• Advantages to saving as text– Preserves white space– Preserves xml declaration– Easily retrieved as a string and

reconverted to xml• Disadvantages

– Can’t be searched directly– Can’t be directly updated– Can’t be tested against a schema

Page 15: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

XML Data Type

• Limit 2Gbs • Strips xml declaration• Can be fragment or whole document• Must be well formed• Can be validated against a schema

store in an XML Schema Collection

Page 16: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

Validating XML (Schema Collections)

• Schema collects are stored in the server and can be used to validate columns with xml data types

Page 17: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

XQuery

• SQL Server fully incorporates XQuery standards allowing a user to directly query xml content stored in the XML data type columns

Page 18: XML in SQL Server 2005. Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form

SqlXml

• SQL XML, consists of a set of tools for processing and manipulating SQL server Data programmically