The XML data type in Microsoft SQL Server. The XML Type Microsoft SQL Server offers a special data type XML Used in tables, etc. Basically a (long)

  • Published on

  • View

  • Download


  • Slide 1
  • The XML data type in Microsoft SQL Server
  • Slide 2
  • The XML Type Microsoft SQL Server offers a special data type XML Used in tables, etc. Basically a (long) text string With some special features Example, create table Create table course ( courseID int identity(1,1) primary key, coursename varchar(50) not null, description xml) An attribute with the XML type can be NULL or NOT NULL Example, insert insert into course (coursename, description) values ('Accounting', ' Accounting blah blah '); 2The XML data type
  • Slide 3
  • Typed vs. non-typed XML Typed XML The XML data type has an associated XML schema The DBMS verifies that the XML input validates using the schema at INSERT and UPDATE Non-typed XML The XML data type does not have an associated XML schema The DBMS verifies that the XML input is well formed at INSERT AND UPDATE 3The XML data type
  • Slide 4
  • Creating an XML schema collection create xml schema collection departmentDescriptionCollection as ' ' 4The XML data type
  • Slide 5
  • Using an XML schema collection Example, CREATE CREATE TABLE department( departmentID int IDENTITY(1,1) NOT NULL, departmentName varchar(50) NOT NULL, description xml(CONTENT departmentDescriptionCollection) Example, INSERT insert into department (departmentName, description) values ('accounting', ' Accounting blah blah '); 5The XML data type
  • Slide 6
  • XML Data type methods The XML data type has 5 methods .query() Using XQuery .value() Access values inside a specific element or attribute .modify() Modify the XML data Not part of the XQuery standard .nodes() Break XML data into relational-style rows .exists() Tests whether a specific kind of data exists The XML data type6
  • Slide 7
  • XQuery Quotes XQuery is to XML, what SQL is to database tables. XQuery was designed to query XML data. From The XML data type7
  • Slide 8
  • Simple XQuery expressions Executed on a variable, not a table column declare @xmlOrderData xml; set @xmlOrderData = N' 250 GB hard drive 120 USB Mouse 25 ' select @xmlOrderData.query('/items/item[@id="25"]'); Source: Lee & Bieker: Mastering Microsoft SQL Server 2008, Sybex /Wiley 2009, page 208-209 8The XML data type
  • Slide 9
  • Simple XQuery Executed on a database column Example, SELECT with XQuery select name, description.query('/root/shortDescription') from course; The XML data type9
  • Slide 10
  • XML.value() method.query() vs.value() select departmentName, description.query('/root/shortDescription') from department; Returns departmentName + XML fragment select departmentName, description.value('/root/shortDescription', 'varchar(50)') from department; Returns departmentName + description Description is converted into a varchar(50).value(XQuery, SQLtype) The Xquery must return at most one value. Syntax often used ()[1] Only the fist element is considered. The XML data type10
  • Slide 11
  • XML.modify() method XQuery can only select data Not modify data Microsoft has extended XQuery to include modification insert delete replace value of Example, UPDATE replace value of UPDATE department SET description.modify('replace value of (/root/shortDescription) with "another description"') WHERE departmentid = 9 The XML data type11
  • Slide 12
  • XML.nodes() method Breaking XML data into their own table This temporary table is often joined with the table that has the XML column. Syntax SELECT columns FROM table CROSS APPLY columnName.nodes(XQuery) AS alias CROSS APPLY is a kind of join The XML data type12
  • Slide 13
  • XML.exist() method Similar to the EXISTS operator in ordinary SQL. XML.exist(XQuery) Returns 1 (true) or 0 (false)! Used in SELECT WHERE Example SELECT departmentName FROM department WHERE description.exist('/root/shortDescription') = 1; The XML data type13


View more >