SlideShare a Scribd company logo
XML Features in SQL Server 2005   David Silverlight [MVP XML] [email_address]
Overview XML Enhancements in SQL Server 2005 The xml Data Type Using XQuery
SQL Server 2005 XML Enhancements  Enhancements to the FOR XML Clause Enhancements to the OPENXML Function
Enhancements to the FOR XML Clause You can specify a root element for your results. ROOT directive You can use XPath-like expressions to define  XML results. PATH mode Results from FOR XML queries can be xml values, making nested queries possible. TYPE directive returns results as xml data type Description Enhancement Element-centric results can include empty elements for null values.  Support for NULL values RAW mode queries can return element-centric XML. ELEMENTS directive in RAW mode You can generate inline XSD schemas. Inline XSD schemas
Demos: Using the FOR XML Clause Return elements in RAW mode Return NULL values Return an inline XSD schema Use the TYPE directive Use PATH mode
Enhancements to the OPENXML Function Description Enhancement Columns of type xml can be returned in the WITH clause.  Support for xml data type in the WITH clause The sp_xml_preparedocument stored procedure accepts xml parameters. Documents can be xml data type values Document handles are scoped at the batch level and are released when the batch is completed. Batch-level scoping
The xml Data Type Storing XML in the Database How to Use Untyped XML How to Use Typed XML How to Manage XML Schemas
Storing XML in the Database Benefits: Single store for structured and semistructured data Define variable content in a relational model Choose the most appropriate data model Functionality: XML Indexes XQuery-based data retrieval XQuery-based modifications XML schema support: Typed XML: Validated by a schema Untyped XML: Nonvalidated XML
How to Use Untyped XML CREATE TABLE Invoices (InvoiceID int, SalesDate datetime, CustomerID int, ItemList xml) DECLARE @itemDoc xml SET @itemDoc = '<Items>etc.</Items>' SET @itemDoc =  CAST('<Items>etc.</Items>') AS xml SET @itemDoc =  CONVERT(xml,'<Items>etc.</Items>') SET @itemDoc =  CONVERT(xml,'<Items>etc.') ERROR! Declare xml data type Implicitly cast string values Explicitly cast string values Use well-formed XML Explicitly convert string values
How to Manage XML Schemas CREATE XML SCHEMA COLLECTION cvSchemas AS N'<?xml version=&quot;1.0&quot; ?> <xsd:schema targetNamespace= ...' SELECT *  FROM sys.xml_schema_collections SELECT *  FROM sys.xml_namespaces ALTER XML SCHEMA COLLECTION cvSchemas DROP XML SCHEMA COLLECTION cvSchemas Create an XML schema collection View schema information Modify a schema collection Remove a schema collection
How to Use Typed XML Declare a typed column or variable CREATE TABLE HumanResources.EmployeeResume (EmployeeID int, Resume xml (cvSchemas)) INSERT INTO HumanResources.EmployeeResume  VALUES  (1,  '<?xml version=&quot;1.0&quot; ?> <resume xmlns=&quot;http://cvSchemas&quot;> ... </resume>') Assign typed XML (must conform to schema) Use CONTENT or DOCUMENT to allow/disallow fragments CREATE TABLE HumanResources.EmployeeResume (EmployeeID int, Resume xml (DOCUMENT cvSchemas))
Demos: Using Typed XML Create an XML schema collection View XML schema collections View XML namespaces Create a table with a typed xml column Insert valid XML Attempt to insert invalid XML Use the CONTENT and DOCUMENT keywords
Using XQuery What Is XQuery? How to Query XML with xml Data Type Methods How to Modify XML with the modify Method How to Shred XML to Relational Format with the Nodes Method
What Is XQuery? Query language to identify nodes in XML /InvoiceList/Invoice[@InvoiceNo=1000] FLOWR statements ( For, Let, Order by, Where and Return ) Description Statement Apply filtering criteria to the iteration. where Iterate through sibling nodes. for Specify the XML to be returned. return
Demos: Using XQuery Expressions Examine an XML document Use simple XQuery path expressions Use an XQuery condition Use the for and return statements Generate literal XML Return attributes and values Use for, where, and return statements
How to Query XML with xml Data Type Methods SELECT xmlCol.query( '<InvoiceNumbers> { for $i in /InvoiceList/Invoice return <InvoiceNo> {number($i/@InvoiceNo)} </InvoiceNo> } </InvoiceNumbers>') SELECT xmlCol.value( '(/InvoiceList/Invoice/@InvoiceNo)[1]', 'int') SELECT xmlCol.exist( '/InvoiceList/Invoice[@InvoiceNo=1000]' ) SELECT Invoices.query( '<Store> {sql:column(&quot;StoreName&quot;)} </Store>') Use the query method Use the value method Bind relational columns and variables Use the exist method
How to Modify XML with the modify Method SET @xmlDoc.modify( 'insert element salesperson {&quot;Bill&quot;} as first into (/InvoiceList/Invoice)[1]') SET xmlCol.modify( replace value of (/InvoiceList/Invoice/SalesPerson/text())[1] with &quot;Ted&quot;') SET @xmlDoc.modify( 'delete  (/InvoiceList/Invoice/SalesPerson)[1]') Use the insert statement Use the replace statement Use the delete statement
What we have learned XML is thoroughly integrated into SQL Server 2005. For XML and OpenXML have seen become much more powerful. Schemas are respected as part of table updates and definitions XQuery is a very powerful query language that allows you to Query your existing data types.
Thank You!!! David Silverlight [email_address] http://www.xmlpitstop.com http://www.nonprofitways.com

More Related Content

DOC
Sql functions
G C Reddy Technologies
 
PDF
Introduction to oracle functions
Nitesh Singh
 
PDF
Introduction To Oracle Sql
Ahmed Yaseen
 
PPTX
Oracle: Basic SQL
DataminingTools Inc
 
PPTX
Structure query language (sql)
Nalina Kumari
 
PPT
Advanced Sql Training
bixxman
 
PPTX
Introduction to SQL
Mahir Haque
 
DOCX
Exploring collections with example
pranav kumar verma
 
Sql functions
G C Reddy Technologies
 
Introduction to oracle functions
Nitesh Singh
 
Introduction To Oracle Sql
Ahmed Yaseen
 
Oracle: Basic SQL
DataminingTools Inc
 
Structure query language (sql)
Nalina Kumari
 
Advanced Sql Training
bixxman
 
Introduction to SQL
Mahir Haque
 
Exploring collections with example
pranav kumar verma
 

What's hot (20)

PPT
Ms sql server ii
Iblesoft
 
PPT
MS SQL Server 1
Iblesoft
 
PPTX
Oracle: Functions
DataminingTools Inc
 
PPTX
SQL Data Manipulation
khalid alkhafagi
 
PPT
Android ListView and Custom ListView
Sourabh Sahu
 
PPTX
Android styles and themes
Sourabh Sahu
 
PPTX
SQL
Jerin John
 
PDF
Reason - introduction to language and its ecosystem | Łukasz Strączyński
Grand Parade Poland
 
PPTX
Lab1 select statement
Balqees Al.Mubarak
 
PDF
JavaScript: Variables and Functions
Jussi Pohjolainen
 
ODP
BIS05 Introduction to SQL
Prithwis Mukerjee
 
PPTX
SQL
Vineeta Garg
 
PPT
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
PPTX
Mimsy XG Resource Session
Axiell ALM
 
PPTX
Sql.pptx
TanishaKochak
 
PPTX
Sql operator
Pooja Dixit
 
PPT
Sql server select queries ppt 18
Vibrant Technologies & Computers
 
PPTX
Where conditions and Operators in SQL
Raajendra M
 
PPTX
Oracle basic queries
PRAKHAR JHA
 
PPTX
SQL Assessment Command Statements
Shaun Wilson
 
Ms sql server ii
Iblesoft
 
MS SQL Server 1
Iblesoft
 
Oracle: Functions
DataminingTools Inc
 
SQL Data Manipulation
khalid alkhafagi
 
Android ListView and Custom ListView
Sourabh Sahu
 
Android styles and themes
Sourabh Sahu
 
Reason - introduction to language and its ecosystem | Łukasz Strączyński
Grand Parade Poland
 
Lab1 select statement
Balqees Al.Mubarak
 
JavaScript: Variables and Functions
Jussi Pohjolainen
 
BIS05 Introduction to SQL
Prithwis Mukerjee
 
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
Mimsy XG Resource Session
Axiell ALM
 
Sql.pptx
TanishaKochak
 
Sql operator
Pooja Dixit
 
Sql server select queries ppt 18
Vibrant Technologies & Computers
 
Where conditions and Operators in SQL
Raajendra M
 
Oracle basic queries
PRAKHAR JHA
 
SQL Assessment Command Statements
Shaun Wilson
 
Ad

Similar to Sql2005 Xml (20)

DOCX
Sql server 2008 r2 xml wp
Klaudiia Jacome
 
PPT
2310 b 12
Krazy Koder
 
PPT
Sql Summit Clr, Service Broker And Xml
David Truxall
 
PPTX
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Ayes Chinmay
 
PPT
Rendering XML Documents
yht4ever
 
PPT
5 xsl (formatting xml documents)
gauravashq
 
PPT
Rendering XML Document
yht4ever
 
PPT
XML stands for EXtensible Markup Language
NetajiGandi1
 
PPT
XMLLec1.pptsfsfsafasfasdfasfdsadfdsfdf dfdsfds
Kamrankhan925215
 
PPT
XMLLec1 (1xML lecturefsfsdfsdfdsfdsfsdfsdfdsf
Kamrankhan925215
 
PPTX
Xml schema
Akshaya Akshaya
 
PPT
Xml and Co.
Findik Dervis
 
PPTX
Oracle Developer Day, 20 October 2009, Oracle De Meern, Holland: Oracle Datab...
Marco Gralike
 
PPTX
SQLPASS AD501-M XQuery MRys
Michael Rys
 
PPT
Bcp
LearningTech
 
PDF
Sql
Diana Diana
 
PDF
Session06 handling xml data
kendyhuu
 
PPT
RESTful Services
Kurt Cagle
 
PPT
crystal xcelsius and web services by dashboardcafe.com
Sigit Yunanto
 
PPT
Application development using Microsoft SQL Server 2000
webhostingguy
 
Sql server 2008 r2 xml wp
Klaudiia Jacome
 
2310 b 12
Krazy Koder
 
Sql Summit Clr, Service Broker And Xml
David Truxall
 
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Ayes Chinmay
 
Rendering XML Documents
yht4ever
 
5 xsl (formatting xml documents)
gauravashq
 
Rendering XML Document
yht4ever
 
XML stands for EXtensible Markup Language
NetajiGandi1
 
XMLLec1.pptsfsfsafasfasdfasfdsadfdsfdf dfdsfds
Kamrankhan925215
 
XMLLec1 (1xML lecturefsfsdfsdfdsfdsfsdfsdfdsf
Kamrankhan925215
 
Xml schema
Akshaya Akshaya
 
Xml and Co.
Findik Dervis
 
Oracle Developer Day, 20 October 2009, Oracle De Meern, Holland: Oracle Datab...
Marco Gralike
 
SQLPASS AD501-M XQuery MRys
Michael Rys
 
Session06 handling xml data
kendyhuu
 
RESTful Services
Kurt Cagle
 
crystal xcelsius and web services by dashboardcafe.com
Sigit Yunanto
 
Application development using Microsoft SQL Server 2000
webhostingguy
 
Ad

More from jason hu 金良胡 (20)

PPT
新员工培训
jason hu 金良胡
 
PDF
Javascript 闭包
jason hu 金良胡
 
PDF
Windows Powershell En
jason hu 金良胡
 
PDF
正则表达式
jason hu 金良胡
 
PDF
Work In Japan
jason hu 金良胡
 
PDF
Linuxcommand
jason hu 金良胡
 
PDF
Ubunturef
jason hu 金良胡
 
PDF
Asp.Net运行时
jason hu 金良胡
 
PDF
Fwunixref
jason hu 金良胡
 
PDF
X Query
jason hu 金良胡
 
PDF
Ms Ajax Dom Element Class
jason hu 金良胡
 
PDF
Ms Ajax Dom Element Class
jason hu 金良胡
 
PDF
Ms Ajax Number And Error Extensions
jason hu 金良胡
 
PDF
Ms Ajax Dom Event Class
jason hu 金良胡
 
PDF
Ms Ajax Date And Boolean Extensions
jason hu 金良胡
 
PDF
Ms Ajax Array Extensions
jason hu 金良胡
 
PDF
Ms Ajax String And Object Extensions
jason hu 金良胡
 
PPT
Ext Js Events
jason hu 金良胡
 
PPT
Ext Js Dom Navigation
jason hu 金良胡
 
PPT
Ext Js Events
jason hu 金良胡
 
新员工培训
jason hu 金良胡
 
Javascript 闭包
jason hu 金良胡
 
Windows Powershell En
jason hu 金良胡
 
正则表达式
jason hu 金良胡
 
Work In Japan
jason hu 金良胡
 
Linuxcommand
jason hu 金良胡
 
Asp.Net运行时
jason hu 金良胡
 
Ms Ajax Dom Element Class
jason hu 金良胡
 
Ms Ajax Dom Element Class
jason hu 金良胡
 
Ms Ajax Number And Error Extensions
jason hu 金良胡
 
Ms Ajax Dom Event Class
jason hu 金良胡
 
Ms Ajax Date And Boolean Extensions
jason hu 金良胡
 
Ms Ajax Array Extensions
jason hu 金良胡
 
Ms Ajax String And Object Extensions
jason hu 金良胡
 
Ext Js Events
jason hu 金良胡
 
Ext Js Dom Navigation
jason hu 金良胡
 
Ext Js Events
jason hu 金良胡
 

Recently uploaded (20)

PPTX
Keynote: CATHOLIC UNIVERSITY: A PLACE OF CREATIVITY AND KNOWLEDGE
Alvaro Barbosa
 
PDF
Tariff Surcharge and Price Increase Decision
Joshua Gao
 
PPTX
Chapter 3 Distributive Negotiation: Claiming Value
badranomar1990
 
PDF
NewBase 26 July 2025 Energy News issue - 1806 by Khaled Al Awadi_compressed.pdf
Khaled Al Awadi
 
PDF
Danielle Oliveira New Jersey - A Seasoned Lieutenant
Danielle Oliveira New Jersey
 
PDF
Keppel Ltd. 1H 2025 Results Presentation Slides
KeppelCorporation
 
PDF
Rodolfo Belcastro su All Around The Worlds Magazine - Febbraio 2025
Rodolfo Belcastro
 
PDF
MDR Services – 24x7 Managed Detection and Response
CyberNX Technologies Private Limited
 
PPTX
Is Your Brand Ready for Expansion? A Strategic Guide to Scaling Successfully
RUPAL AGARWAL
 
PDF
Unveiling the Latest Threat Intelligence Practical Strategies for Strengtheni...
Auxis Consulting & Outsourcing
 
PDF
William Trowell - A Construction Project Manager
William Trowell
 
PPTX
Integrative Negotiation: Expanding the Pie
badranomar1990
 
PPTX
E-Way Bill under GST – Transport & Logistics.pptx
Keerthana Chinnathambi
 
PPTX
Social Media Marketing for Business Growth
vidhi622006
 
PDF
askOdin - An Introduction to AI-Powered Investment Judgment
YekSoon LOK
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Bihar Idea festival - Pitch deck-your story.pdf
roharamuk
 
PPTX
E-commerce and its impact on business.
pandeyranjan5483
 
PPTX
What is Letter Writing, Letter Writing Sample
SeemaAgrawal43
 
PDF
bain-temasek-sea-green-economy-2022-report-investing-behind-the-new-realities...
YudiSaputra43
 
Keynote: CATHOLIC UNIVERSITY: A PLACE OF CREATIVITY AND KNOWLEDGE
Alvaro Barbosa
 
Tariff Surcharge and Price Increase Decision
Joshua Gao
 
Chapter 3 Distributive Negotiation: Claiming Value
badranomar1990
 
NewBase 26 July 2025 Energy News issue - 1806 by Khaled Al Awadi_compressed.pdf
Khaled Al Awadi
 
Danielle Oliveira New Jersey - A Seasoned Lieutenant
Danielle Oliveira New Jersey
 
Keppel Ltd. 1H 2025 Results Presentation Slides
KeppelCorporation
 
Rodolfo Belcastro su All Around The Worlds Magazine - Febbraio 2025
Rodolfo Belcastro
 
MDR Services – 24x7 Managed Detection and Response
CyberNX Technologies Private Limited
 
Is Your Brand Ready for Expansion? A Strategic Guide to Scaling Successfully
RUPAL AGARWAL
 
Unveiling the Latest Threat Intelligence Practical Strategies for Strengtheni...
Auxis Consulting & Outsourcing
 
William Trowell - A Construction Project Manager
William Trowell
 
Integrative Negotiation: Expanding the Pie
badranomar1990
 
E-Way Bill under GST – Transport & Logistics.pptx
Keerthana Chinnathambi
 
Social Media Marketing for Business Growth
vidhi622006
 
askOdin - An Introduction to AI-Powered Investment Judgment
YekSoon LOK
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Bihar Idea festival - Pitch deck-your story.pdf
roharamuk
 
E-commerce and its impact on business.
pandeyranjan5483
 
What is Letter Writing, Letter Writing Sample
SeemaAgrawal43
 
bain-temasek-sea-green-economy-2022-report-investing-behind-the-new-realities...
YudiSaputra43
 

Sql2005 Xml

  • 1. XML Features in SQL Server 2005 David Silverlight [MVP XML] [email_address]
  • 2. Overview XML Enhancements in SQL Server 2005 The xml Data Type Using XQuery
  • 3. SQL Server 2005 XML Enhancements Enhancements to the FOR XML Clause Enhancements to the OPENXML Function
  • 4. Enhancements to the FOR XML Clause You can specify a root element for your results. ROOT directive You can use XPath-like expressions to define XML results. PATH mode Results from FOR XML queries can be xml values, making nested queries possible. TYPE directive returns results as xml data type Description Enhancement Element-centric results can include empty elements for null values. Support for NULL values RAW mode queries can return element-centric XML. ELEMENTS directive in RAW mode You can generate inline XSD schemas. Inline XSD schemas
  • 5. Demos: Using the FOR XML Clause Return elements in RAW mode Return NULL values Return an inline XSD schema Use the TYPE directive Use PATH mode
  • 6. Enhancements to the OPENXML Function Description Enhancement Columns of type xml can be returned in the WITH clause. Support for xml data type in the WITH clause The sp_xml_preparedocument stored procedure accepts xml parameters. Documents can be xml data type values Document handles are scoped at the batch level and are released when the batch is completed. Batch-level scoping
  • 7. The xml Data Type Storing XML in the Database How to Use Untyped XML How to Use Typed XML How to Manage XML Schemas
  • 8. Storing XML in the Database Benefits: Single store for structured and semistructured data Define variable content in a relational model Choose the most appropriate data model Functionality: XML Indexes XQuery-based data retrieval XQuery-based modifications XML schema support: Typed XML: Validated by a schema Untyped XML: Nonvalidated XML
  • 9. How to Use Untyped XML CREATE TABLE Invoices (InvoiceID int, SalesDate datetime, CustomerID int, ItemList xml) DECLARE @itemDoc xml SET @itemDoc = '<Items>etc.</Items>' SET @itemDoc = CAST('<Items>etc.</Items>') AS xml SET @itemDoc = CONVERT(xml,'<Items>etc.</Items>') SET @itemDoc = CONVERT(xml,'<Items>etc.') ERROR! Declare xml data type Implicitly cast string values Explicitly cast string values Use well-formed XML Explicitly convert string values
  • 10. How to Manage XML Schemas CREATE XML SCHEMA COLLECTION cvSchemas AS N'<?xml version=&quot;1.0&quot; ?> <xsd:schema targetNamespace= ...' SELECT * FROM sys.xml_schema_collections SELECT * FROM sys.xml_namespaces ALTER XML SCHEMA COLLECTION cvSchemas DROP XML SCHEMA COLLECTION cvSchemas Create an XML schema collection View schema information Modify a schema collection Remove a schema collection
  • 11. How to Use Typed XML Declare a typed column or variable CREATE TABLE HumanResources.EmployeeResume (EmployeeID int, Resume xml (cvSchemas)) INSERT INTO HumanResources.EmployeeResume VALUES (1, '<?xml version=&quot;1.0&quot; ?> <resume xmlns=&quot;http://cvSchemas&quot;> ... </resume>') Assign typed XML (must conform to schema) Use CONTENT or DOCUMENT to allow/disallow fragments CREATE TABLE HumanResources.EmployeeResume (EmployeeID int, Resume xml (DOCUMENT cvSchemas))
  • 12. Demos: Using Typed XML Create an XML schema collection View XML schema collections View XML namespaces Create a table with a typed xml column Insert valid XML Attempt to insert invalid XML Use the CONTENT and DOCUMENT keywords
  • 13. Using XQuery What Is XQuery? How to Query XML with xml Data Type Methods How to Modify XML with the modify Method How to Shred XML to Relational Format with the Nodes Method
  • 14. What Is XQuery? Query language to identify nodes in XML /InvoiceList/Invoice[@InvoiceNo=1000] FLOWR statements ( For, Let, Order by, Where and Return ) Description Statement Apply filtering criteria to the iteration. where Iterate through sibling nodes. for Specify the XML to be returned. return
  • 15. Demos: Using XQuery Expressions Examine an XML document Use simple XQuery path expressions Use an XQuery condition Use the for and return statements Generate literal XML Return attributes and values Use for, where, and return statements
  • 16. How to Query XML with xml Data Type Methods SELECT xmlCol.query( '<InvoiceNumbers> { for $i in /InvoiceList/Invoice return <InvoiceNo> {number($i/@InvoiceNo)} </InvoiceNo> } </InvoiceNumbers>') SELECT xmlCol.value( '(/InvoiceList/Invoice/@InvoiceNo)[1]', 'int') SELECT xmlCol.exist( '/InvoiceList/Invoice[@InvoiceNo=1000]' ) SELECT Invoices.query( '<Store> {sql:column(&quot;StoreName&quot;)} </Store>') Use the query method Use the value method Bind relational columns and variables Use the exist method
  • 17. How to Modify XML with the modify Method SET @xmlDoc.modify( 'insert element salesperson {&quot;Bill&quot;} as first into (/InvoiceList/Invoice)[1]') SET xmlCol.modify( replace value of (/InvoiceList/Invoice/SalesPerson/text())[1] with &quot;Ted&quot;') SET @xmlDoc.modify( 'delete (/InvoiceList/Invoice/SalesPerson)[1]') Use the insert statement Use the replace statement Use the delete statement
  • 18. What we have learned XML is thoroughly integrated into SQL Server 2005. For XML and OpenXML have seen become much more powerful. Schemas are respected as part of table updates and definitions XQuery is a very powerful query language that allows you to Query your existing data types.
  • 19. Thank You!!! David Silverlight [email_address] http://www.xmlpitstop.com http://www.nonprofitways.com