Query XML data from a table with XML data type

The purpose of this article is to guide Microsoft SQL Server database administrators in:

• Create XML schema (XML Shema).
• Create a table with XML data type.
• Import XML file into table with XML data type.
• Query XML file.
• Query the XML file and output the result, similar to the output from Transact SQL Statement commands.

Step 1

First, create a C file: XMLCustomer1.XML as shown below. This XML file contains data related to a customer.




2007-03-31T06: 40: 38.0000000-05: 00
james.brewer
1AE
A-Accessible
761
Stopped
30


2007-03-31T06: 40: 38.0000000-05: 00
james.brewer
1AE
Not-Accessible
870
Stopped
30


2007-03-31T06: 40: 38.0000000-05: 00
james.brewer
1AE
A-Accessible
97F
Started
30

Step 2

Create a database (database) and a set of XML Schema as below:

 USE [master] GO / ****** Object: Database [XMLTest] Script Date: 04/17/2007 01:49:43 ****** / IF EXISTS (SELECT name FROM sys.databases WHERE name = N'XMLTest ') DROP DATABASE [XMLTest] go to create database XMLTest go to use XMLTest Create XML Schema Collection XMLTrack as N' 'go 

Note : updating the Schema collection is based on your own data in the XML file.

Step 3

Create a table with XML data type:

USE [XMLTest]
GO
/ ****** Object: Table [dbo]. [XMLFiles] Script Date: 04/17/2007 02:07:52 ****** /
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID (N '[dbo]. [XMLFiles]') AND type in (N'U '))
DROP TABLE [dbo]. [XMLFiles]

create table XMLFiles (Fileid int identity (1,1),
ImportedDate datetime constraint xmldatestamp default getdate (),
Filename varchar (500),
data xml (XMLTrack))

Step 4

Enter the XML file you just created (C: XMLCustomer1.XML), using the openrowset function as shown below:

USE [XMLTest]
go
INSERT INTO XMLFiles (Filename, DATA)
SELECT 'Customer1' a, *
FROM OPENROWSET (BULK 'C: XMLCustomer1.xml', SINGLE_CLOB)
as mytable
go

Note : The SINGLE_BLOB keyword will import the entire XML file for a column with an XML data type.

Step 5

Query the XMLFiles table, using the SQL operations shown below:

 USE [XMLTest] go select * from XMLFiles where FileId = 1 go 

This command will produce the following results:

Query XML data from a table with XML data type Picture 1
Figure 1.0

When you click on the data, it also displays XML data (Figure 1.1).

 2007-03-3106: 40: 38.0000-05: 00 james.brewer 1AE A-Accessible 761 Stopped 30 2007-03-31T06: 40: 38.0000000-05: 00 james.brewer 1AE Not-Accessible 870 Stopped 30 2007-03- 31T06: 40: 3800000-05: 00 james.brewer 1AE A-Accessible 97F Started 30 

Query XML data from a table with XML data type Picture 2
Figure 1.1

Step 6

Now, query the XML data from the table to produce an SQL transaction as the result set. Execute XQuery program as below:

SELECT
ref.value ('Date', 'nvarchar (364)') as [Date],
ref.value ('user', 'nvarchar (364)') as [User],
ref.value ('Userid', 'nvarchar (364)') as [Userid],
ref.value ('ls', 'nvarchar (364)') as [ls],
ref.value ('eqtid', 'nvarchar (364)') as [eqtid],
ref.value ('es', 'nvarchar (364)') as [es],
ref.value ('tp', 'nvarchar (364)') as [tp]
FROM XMLFiles CROSS APPLY Data.nodes ('// Customer / CustomerLogInfo') R (ref)
where Fileid = 1

The program will produce results as shown in Figure 1.2:

 Date, User, Userid, ls, eqtid, es, tp 2007-03-3106: 40: 38.0000000-05: 00, james.brewer, 1AE, A-Accessible, 761, Stopped, 30 2007-03-31T06: 40: 38.0000000-05: 00, james.brewer, 1AE, Not-Accessible, 870, Stopped, 30 2007-03-31T06: 40: 38.0000000-05: 00, james.brewer, 1AE, A-Accessible, 97F, Started, 30 

Query XML data from a table with XML data type Picture 3
Figure 1.2

Step 7

Now repeat step 4 and re-enter the data.

 USE [XMLTest] go INSERT INTO XMLFiles (Filename, DATA) SELECT 'Customer1' a, * FROM OPENROWSET (BULK 'C: XMLCustomer1.xml', SINGLE_CLOB) as mytable go 

Step 8

Query the table as shown below:

 USE [XMLTest] go select * from XMLFiles go 

The result is:

Query XML data from a table with XML data type Picture 4
Figure 1.3

Step 9

To display all data from both rows, we can write the query as shown below:

SELECT
ref.value ('Date', 'nvarchar (364)') as [Date],
ref.value ('user', 'nvarchar (364)') as [User],
ref.value ('Userid', 'nvarchar (364)') as [Userid],
ref.value ('ls', 'nvarchar (364)') as [ls],
ref.value ('eqtid', 'nvarchar (364)') as [eqtid],
ref.value ('es', 'nvarchar (364)') as [es],
ref.value ('tp', 'nvarchar (364)') as [tp]
FROM XMLFiles CROSS APPLY Data.nodes ('// Customer / CustomerLogInfo') R (ref)
where Fileid = 1
union all
SELECT
ref.value ('Date', 'nvarchar (364)') as [Date],
ref.value ('user', 'nvarchar (364)') as [User],
ref.value ('Userid', 'nvarchar (364)') as [Userid],
ref.value ('ls', 'nvarchar (364)') as [ls],
ref.value ('eqtid', 'nvarchar (364)') as [eqtid],
ref.value ('es', 'nvarchar (364)') as [es],
ref.value ('tp', 'nvarchar (364)') as [tp]
FROM XMLFiles CROSS APPLY Data.nodes ('// Customer / CustomerLogInfo') R (ref)
where Fileid = 2

The results are in the form of Figure 1.4.

 Date, User, Userid, ls, eqtid, es, tp 2007-03-3106: 40: 38.0000000-05: 00, james.brewer, 1AE, A-Accessible, 761, Stopped, 30 2007-03-31T06: 40: 38.0000000-05: 00, james.brewer, 1AE, Not-Accessible, 870, Stopped, 30 2007-03-31T06: 40: 38.0000000-05: 00, james.brewer, 1AE, A-Accessible, 97F, Started, 30 2007-03-3106: 40: 38.0000000-05: 00, james.brewer, 1AE, A-Accessible, 761, Stopped, 30 2007-03-31T06: 40: 38.0000000-05: 00, james.brewer, 1AE, Not -Accessible, 870, Stopped, 30 2007-03-31T06: 40: 38.0000000-05: 00, james.brewer, 1AE, A-Accessible, 97F, Started, 30 

Note: If you are planning to display all data from each row with an XML data type, you can create a stored procedure with a temporary table or pointer .

Query XML data from a table with XML data type Picture 5
Figure 1.4

Conclude

This article illustrates how to create an XML Schema, create a table with an XML data type, import an XML file into a table with an XML data type, query the XML file, and produce the same result as the actual result set. shown by SQL Transact commands.

4 ★ | 1 Vote

May be interested

  • Improvements in ASMPhoto of Improvements in ASM
    asm enhances the performance provided by raw disks, making it difficult to administer these disks. in addition, asm provides storage mapping policy, which can be configured at the file level instead of the drive level according to the retrieval method.
  • The ten biggest errors developers have with databasesPhoto of The ten biggest errors developers have with databases
    the software world is constantly evolving with new, more fashionable, more elegant, more functional models. it has been a long time since it products were just 'lousy' machines, having every task performed by the coders' coding functions.
  • Use and manage Database MailPhoto of Use and manage Database Mail
    sql server 2005 has a small mail system called database mail. as an improved feature in sql mail compared to earlier versions of sql server, database mail is a mail queue system. email messages are stored in an internal queue
  • Processing data in Java in OraclePhoto of Processing data in Java in Oracle
    is oracle always the best tool or application to handle data? the answer lies inside what you do. typically, data is transformed through the data processing language, but it can also be transformed through processing strings or numbers.
  • Create .Net applications on Linux and Mac OS XPhoto of Create .Net applications on Linux and Mac OS X
    mono is an open source deployment of .net framework built on linux, solaris, mac os x, windows and unix.
  • CASE function in SQL Server (part 1)Photo of CASE function in SQL Server (part 1)
    in sql server, the case function verifies the value based on the list of given conditions, then returns one or more results. in this article we will illustrate a number of different uses of this function in different situations.