This article discusses the new features of SQL Server 2005, which allows you to decompose XML data into relational formats without having to consume too much memory. Let's first take a look at XQuery and the functionality it provides to developers in SQL Server 2005.
About XQuery
XQuery, also known as XML Query, is a l
Generate XML data for the table
SelectSiteid,GoodsareaFromTableFor XML Path
Convert XML into a table
Method 1 nodes Function
Declare @ XMLSet @ xml ='SelectDoc. Col. Value ('@ ponumber', 'varchar (10)') ponumber, D
Label:Often in the forum to see the master using the FOR XML path, because it is a search, recorded a detailed use of the method.Using the FOR XML PATH statement in SQL Server to generate XML data for the queried data, here are some examples of its application. DECLARE @TempTable table (UserID int, UserName nvarchar (5
As an example of how to decompose the data in the previous article, we first look at XQuery and the functionality it provides to developers in SQL Server 2005.
About XQuery
XQuery, also known as XML Query, is a language that queries XML data, allowing you to extract the required nodes and elements. It is defined by the consortium and can be used in most of today'
@hobby for XML PATH ('myhobby') So at this point the node name of our column will also be programmed with our custom name Myhobby>
MyCode>1MyCode>
MyName>ClimbingMyName>
Myhobby>
Myhobby>
MyCode>2MyCode>
MyName>SwimmingMyName>
Myhobby>
Myhobby>
MyCode>3MyCode>
MyName>FoodMyName>
Myhobby> Oh! Now that we can customize the nodes and columns of the row, can we build the output that we like? Or look at the code: SELECT ' '+hname+' ]'from@
1. FOR XML path (' str ')Select Id,createtime from dbo. Articleinfo FOR XML Path (' MyTitle ')Result: (Note: If it is select Id,createtime from dbo.) Articleinfo FOR XML path below results 2. FOR XML Path (")Select ' [' +convert (Varchar
DescriptionQuery () method (XML data type)This method is used to query the XML instance.Value () method (XML data type)This method is used to retrieve the value of the SQL type from an XML instance.exist () method (XML data type)T
Sometimes we need to save the data read from the database as XML directly. Here we can achieve this through SQL statements.
First, create a table:Code
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Use [IP]Go/***** Object: Table [DBO]. [sitedata] script Date: 17:41:11 ******/Set ansi_nulls onGoSet quoted_identifier onGoSet ansi_padding onGoCreate Table [
Read Catalogue
Create a test table Insert test data
The FOR XML path uses the
General usage
Custom node Name
property of the field as XML
We usually need to get a string that is spelled out by a field of multiple rows, and we can use the FOR XML path to handle it; The specific use of the FOR
In SQL Server, the for XML Path statement can be used to generate XML data for the queried data. Below are some examples of its application.
Declare @ temptable table (userid int, username nvarchar (50); insert into @ temptable (userid, username) values (1, 'A') insert into @ temp, username) values (2, 'B') Select userid, username from @ temptable for
change? Do you remember the keyword as that lists the aliases? Yes, that's it! The code is as follows:SELECT Hobbyid as ' MyCode ', hname as ' MyName ' from @hobby for XML PATH (' Myhobby ') So at this point the node name of our column will also be programmed with our custom name Oh! Now that we can customize the nodes and columns of the row, can we build the output that we like? Or look at the code:SELECT ' [' +hname+ '] ' from @hobby for
lists the aliases? Yes, that's it! The code is as follows:SELECT Hobbyid as ' MyCode ', hname as ' MyName ' from @hobby for XML PATH (' Myhobby ')So at this point the node name of our column will also be programmed with our custom name Oh! Now that we can customize the nodes and columns of the row, can we build the output that we like? Or look at the code: SELECT ' [' +hname+ '] ' from @hobby for XML PATH
First set up a table:
Copy Code code as follows:
Use [IP]
Go
/****** object: Table [dbo]. [Sitedata] Script Date: 09/11/2010 17:41:11 ******/
SET ANSI_NULLS on
Go
SET QUOTED_IDENTIFIER ON
Go
SET ansi_padding on
Go
CREATE TABLE [dbo]. [Sitedata] (
[ID] [int] IDENTITY (1,1) not NULL,
[SiteName] [varchar] (m) COLLATE chinese_prc_ci_as not NULL,
[Siteadd] [varchar] (MB)
EXEC sp_xml_preparedocument @xmldata_id OUTPUT, @xmldata, '
SELECT @customerid = CustomerID, @customername = [name] FromOPENXML (@xmldata_id, '//customer ', 2) with (Customeridint, [name]varchar (50))
EXEC sp_xml_removedocument @xmldata_id
UPDATE Customer SET Customer. [Name] = ISNULL (@customername, customer.[ Name])WHERE Customer.tblid = @customerid
This process first declares that the variable we are going to use will hold the relevant informatio
your application rarely uses a database to store and retrieve data, the [N]varchar (max) column meets all your needs. Storing data in an XML column can provide additional benefits-the engine checks the data format specification or is valid, and supports fine-grained querying and updating of XML data.
Reasons to store XML
time the careful friend must ask then how does the column node change? Do you remember the keyword as that lists the aliases? Yes, that's it! The code is as follows:SELECT Hobbyid as ' MyCode ', hname as ' MyName ' from @hobby for XML PATH (' Myhobby ') So at this point the node name of our column will also be programmed with our custom name Oh! Now that we can customize the nodes and columns of the row, can we build the output that we like? Or loo
the keyword as that lists the aliases? Yes, that's it! The code is as follows:SELECT Hobbyid as ' MyCode ', hname as ' MyName ' from @hobby for XML PATH (' Myhobby ') So at this point the node name of our column will also be programmed with our custom name Oh! Now that we can customize the nodes and columns of the row, can we build the output that we like? Or look at the code:SELECT ' [' +hname+ '] ' from @hobby for
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.