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:
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
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
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:
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 .
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.
You should read it
May be interested
- 35 tools, scripts and plugins to build HTML Tablethere are many ways to visualize data, ie you can design a beautiful infographics or create interactive charts. it all depends on your data and how you want to present them.
- RIGHT JOIN in SQLright join in sql is a type of join that returns all records from the right side table and matching records from the left side table.
- Why use Microsoft Power Query for Excelif you're looking for a powerful new way to manage data, microsoft excel for windows now has software built in to help you with that, which is microsoft power query. this is a new tool that helps you take control of your spreadsheets more than ever.
- MS Access - Lesson 5: Create a tablethere are two ways to view tables in access and add data to the table: design view and datasheet view.
- MS Access 2003 - Lesson 21: Chapter 6: Using Query to check datain this chapter, you will learn how to filter and organize data, which is a powerful tool provided by access - queries.
- Multiple choice questions have a Query optionquery is a language used in databases and information systems. the following is an invitation to test your knowledge with network administrator through multiple-choice questions below.
- Alternate Criteria in Access 2016in addition to the uses of the tipsmake.com query introduced in the previous lessons, you can also view data and records that meet two or more conditions using the alternative criteria.
- Data type in C / C ++data type in c / c ++ while working with any programming language, you need to use various types of variables to store information. variables, nothing but memory locations are reserved for saving values. that is, when you create a variable, you reserved some space in memory for that variable.
- Set up Query Criteria in Access 2016the query criteria (query criteria) are filter conditions that help you retrieve specific items from an access database, used when you want to limit results based on values in a field.
- Data type in C #value type variables can be assigned a value directly. they are inherited from the system.valuetype class.