Friday, March 4, 2016

Database : SQL Server XML Data Type

Xml data type was introduced in SQL Server 2005 to work with XML data. Using this data type, we can store XML in its native format and can also query/modify the xml data within the xml. We can use xml data type like as:
  1. Variable
  2. Field/Column in a table
  3. Parameter in the user-defined function (UDF) or stored procedure(SP)
  4. return value from a UDF or SP
We can define xml data type field to NOT NULL or we can provide a default value to it.

Limitation Of XML Data type

  1. We can’t directly compare an instance of the XML data type to another instance of the XML data type. For equality comparisons we first need to convert the XML type to a character type.
  2. We can’t use GROUP BY or ORDER BY with an XML data type column.
  3. We can’t use XML data type field as a primary key, Unique key and foreign key.
  4. We can’t define XML data type field with COLLATE keyword.

Query XML Data

Suppose we have following tables in database. Using these tables we will produce query result as an xml
  1. CREATE TABLE Department (
  2. DeptID int IDENTITY(1,1) primary key ,
  3. DeptName varchar(50) NULL,
  4. Location varchar(50) NULL )
  5. CREATE TABLE Employee (
  6. EmpID int IDENTITY(1,1) NOT NULL,
  7. EmpName varchar(50) NULL,
  8. Address varchar(100) NULL,
  9. DeptID int foreign Key references Department(DeptID)
  10. )
  11. --Now Insert data into these tables
  12. INSERT INTO Department (DeptName,Location)VALUES('HR','Delhi')
  13. INSERT INTO Department (DeptName,Location)VALUES('IT','Delhi')
  14. INSERT INTO Department (DeptName,Location)VALUES('Technical','Delhi')
  15. INSERT INTO Employee( EmpName,Address,DeptID)VALUES('Shailendra','Noida',2)
  16. INSERT INTO Employee( EmpName,Address,DeptID)VALUES('Mohan','Noida',2)
  17. INSERT INTO Employee( EmpName,Address,DeptID)VALUES('Vipul','Noida',1)
  18. INSERT INTO Employee( EmpName,Address,DeptID)VALUES('Mrinal','Noida',3)
We can retrieve data table records as xml data using FORXML clause in SELECT statement. In FORXML Clause we can define three xml mode.
  1. AUTO

    It generates output with both element and attribute features in combination with a sub query.
    1. SELECT DeptName, EmpID
    2. FROM Employee AS Emp JOIN Department AS Dept
    3. ON Emp.DeptID= Dept.DeptID
    4. FOR XML AUTO;
    Output:
    1. <Dept DeptName="IT">
    2. <Emp EmpID="1" />
    3. <Emp EmpID="2" />
    4. </Dept>
    5. <Dept DeptName="HR">
    6. <Emp EmpID="3" />
    7. </Dept>
    8. <Dept DeptName="Technical">
    9. <Emp EmpID="4" />
    10. <Emp EmpID="5" />
    11. </Dept>
  2. EXPLICIT

    It converts the rowset that is result of the query execution, into an XML document. This mode provides more control over the format of the XML means in which format you want xml you need to define that format in select query.
    1. SELECT
    2. 1 tag,
    3. NULL parent,
    4. EmpID [employee!1!ID],
    5. EmpName [employee!1!name],
    6. NULL [order!2!date],
    7. NULL [department!3!name]
    8. FROM Employee
    9. UNION ALL
    10. SELECT
    11. 3,
    12. 1,
    13. EmpID,
    14. NULL,
    15. NULL,
    16. DeptName
    17. FROM Employee e JOIN Department d
    18. ON e.DeptID=d.DeptID
    19. ORDER BY 3, 1
    20. FOR XML EXPLICIT;
    Output
    1. <employee ID="1" name="Shailendra">
    2. <department name="IT" />
    3. </employee>
    4. <employee ID="2" name="Mohan">
    5. <department name="IT" />
    6. </employee>
    7. <employee ID="3" name="Vipul">
    8. <department name="HR" />
    9. </employee>
    10. <employee ID="4" name="Mrinal">
    11. <department name="Technical" />
    12. </employee>
    13. <employee ID="5" name="Jitendra">
    14. <department name="Technical" />
    15. </employee>
  3. RAW

    It produce a single element or the optionally provided element name for each row in the query result set that is returned by select statement.
    1. SELECT Emp.EmpID, Dept.DeptName
    2. Employee as Emp JOIN Department as Dept
    3. ON Emp.DeptID= Dept.DeptID
    4. FOR XML RAW;
    Output
    1. <row EmpID="1" DeptName="IT" />
    2. <row EmpID="2" DeptName="IT" />
    3. <row EmpID="3" DeptName="HR" />
    4. <row EmpID="4" DeptName="Technical" />
    5. <row EmpID="5" DeptName="Technical" />
Summary
In this article I try to explain the XML Data Type with example. I hope after reading this article your will be aware of XML Datatype in Sql Server. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

No comments:

Post a Comment