XML DATA and XQUERY IN SQL SERVER

12
XML Data and XQUERY in SQL SERVER Presenter:Bishwaranjan Sandhu, Mindfire Solutions Date: 28/07/2014

description

SQL Server provides a powerful platform for developing rich applications for semi-structured data management. Support of XML is integrated into all the components of SQL SERVER which includes XML data type, ability to specify XQuery on XML data, managing bulk loading of XML data, retrieving relational data in XML format(FOR XML), retrieving XML data in relational format(OPENXML) etc. This session will be beneficial for people with basic knowledge of SQL SERVER.

Transcript of XML DATA and XQUERY IN SQL SERVER

Page 1: XML DATA and XQUERY IN SQL SERVER

XML Data and XQUERY in SQL SERVER

Presenter:Bishwaranjan Sandhu, Mindfire SolutionsDate: 28/07/2014

Page 2: XML DATA and XQUERY IN SQL SERVER

Presenter: Bishwaranjan Sandhu, Mindfire Solutions

I am Bishwaranjan Sandhu, working as a web developer with Mindfire Solutions .I am having 3years of core experience in web Development. I mostly work in .NET technologies like ASP.NET MVC 4.0/5.0, C#, Entity Framework, SQL Server 2008/2012, WCF and having proficient knowledge in MVC 4.0/MVC 5.0, CSS3, HTML5, jQuery and Kendo UI.

I am having three MS Certifications:MCTS 70-515MCTS 70-515 (Web Development) Microsoft Certification ID: 9296947.Microsoft exam 70-480Microsoft exam 70-480 (Programming in HTML5 with JavaScript and CSS3)Microsoft exam 70-486 Microsoft exam 70-486 Developing ASP.NET MVC 4 Web Applications.

Page 3: XML DATA and XQUERY IN SQL SERVER

Presenter: Bishwaranjan Sandhu, Mindfire Solutions

AGENDA

-> Basic Syntax and use of of XML data type-> Basic Syntax of the FOR XML Clause-> Use RAW Mode with FOR XML-> Use AUTO Mode with FOR XML-> Use PATH Mode with FOR XML-> OPENXML-> XQuery on XML data-> Examples

Page 4: XML DATA and XQUERY IN SQL SERVER

Basic Syntax and use of of XML data typeRelational or XML Data ModelRelational data for structured data with schema.XML is a good choice if you want a platform-independent model in order to ensure portability of the data by using structural and semantic markup.Reasons for Storing XML Data in SQL ServerYou want to share, query, and modify your XML data in an efficient and transacted way.You want indexing of XML data for efficient query processing and good scalability, and the use of a first-rate query optimizer.XML Storage OptionsLimitations of the xml Data Type

Presenter: Bishwaranjan Sandhu, Mindfire Solutions

Page 5: XML DATA and XQUERY IN SQL SERVER

Basic Syntax of the FOR XML Clause

ArgumentsRAW[('ElementName')] AUTO EXPLICIT PATH XMLDATAELEMENTS BINARY BASE64 TYPE ROOT [('RootName')]

Presenter:Bishwaranjan Sandhu, Mindfire Solutions

Page 6: XML DATA and XQUERY IN SQL SERVER

Presenter:Bishwaranjan Sandhu , Mindfire Solutions

Use RAW Mode with FOR XML

RAW mode transforms each row in the query result set into an XML element that has the generic identifier <row>, or the optionally provided element name.

Example: Retrieving Product Model Information as XML

Example: Specifying XSINIL with the ELEMENTS Directive

Example: Requesting Schemas as Results with the XMLDATA and XMLSCHEMA Options

Example: Retrieving Binary Data

Example: Renaming the <row> Element

Example: Specifying a Root Element for the XML Generated by FOR XML

Page 7: XML DATA and XQUERY IN SQL SERVER

Use AUTO Mode with FOR XML

Presenter: Bishwaranjan Sandhu, Mindfire Solutions

AUTO mode returns query results as nested XML elements. This does not provide much control over the shape of the XML generated from a query result. The AUTO mode queries are useful if you want to generate simple hierarchies. However, Use EXPLICIT Mode with FOR XML and Use PATH Mode with FOR XML provide more control and flexibility in deciding the shape of the XML from a query result.

Each table in the FROM clause, from which at least one column is listed in the SELECT clause, is represented as an XML element. The columns listed in the SELECT clause are mapped to attributes or subelements, if the optional ELEMENTS option is specified in the FOR XML clause.

Page 8: XML DATA and XQUERY IN SQL SERVER

Use PATH Mode with FOR XML Constructing XML Using FOR XML, the PATH mode provides a simpler way

to mix elements and attributes. PATH mode is also a simpler way to introduce additional nesting for representing complex properties. You can use FOR XML EXPLICIT mode queries to construct such XML from a rowset, but the PATH mode provides a simpler alternative to the potentially cumbersome EXPLICIT mode queries

Presenter: Bishwaranjan Sandhu, Mindfire Solutions

Columns without a NameColumns with a NameColumns with the Name of an XPath Node TestColumns that Contain a Null Value By DefaultExamples: Using PATH Mode

Page 9: XML DATA and XQUERY IN SQL SERVER

OPENXML

Presenter: Bishwaranjan Sandhu, Mindfire Solutions

OPENXML, a Transact-SQL keyword, provides a rowset over in-memory XML documents that is similar to a table or a view. OPENXML allows access to XML data as though it is a relational rowset. It does this by providing a rowset view of the internal representation of an XML document. The records in the rowset can be stored in database tables.

OPENXML Parameters An XML document handle (idoc)

An XPath expression to identify the nodes to be mapped to rows (rowpattern)

A description of the rowset to be generated

Mapping between the rowset columns and the XML nodes

Page 10: XML DATA and XQUERY IN SQL SERVER

Examples

Demo...

Presenter: Bishwaranjan Sandhu, Mindfire Solutions

Page 11: XML DATA and XQUERY IN SQL SERVER

Presenter:Bishwaranjan Sandhu, Mindfire Solutions

Thank you