SQL Server and XML integration (1)

Source: Internet
Author: User
Tags base64 query rowcount
Server|xml SQL Server and XML integration

Author: builder.com
Wednesday, June 2002 10:11 AM



XML is the latest solution for data storage. XML gives web developers greater programming flexibility than HTML. This new technology drives the development organization to try to integrate XML with its own products. Microsoft is the pioneer of such a move. Microsoft has been slow at the time of developing Internet products, but it's different now. "You're going to give us a knife, and it's all XML in the wound," says a senior Microsoft executive. "Maybe it's a bit too much, but it's true: XML is visible in almost all of Microsoft's products." For example: How does Microsoft integrate XML into its SQL Server product line? Let's take a look at the key FOR XML clause below.
Get information in the name of XML

















The first thing about integration between SQL Server and XML is to create an XML file from SQL data. The structure of the XML file is not complex and can be easily generated with simple scripts and an ADO recordset. While this task is not a hassle, developers need to produce different scripts for the result sets they get from the server, or to write more complex generic scripts. The SELECT statement is thus equipped with a new FOR XML clause.

The syntax of the clause is as follows:
[For {XML {RAW | AUTO | EXPLICIT}
[, XMLDATA]
[, ELEMENTS]
[, BINARY BASE64]} ]

The XML schema for the FOR XML clause is represented by three parameter values: RAW, Auto, or explicit. The pattern determines the form and composition of the resulting XML. Let's take a closer look at each of these XML options with the following examples.

Raw sample
We execute the following SQL statement:
SET ROWCOUNT 3
SELECT Orders.OrderID, Orders.orderdate, ProductID
From Orders, [order Details]
WHERE Orders.OrderID = [Order Details]. OrderID
ORDER BY Orders.OrderID
For XML RAW

The following results have been implemented:
<row orderid= "10248" orderdate= "1996-07-04t00:00:00" productid= "one"/>
Orderid= "10248" orderdate= "1996-07-04t00:00:00" productid= "<row"/>
<row orderid= "10248" orderdate= "1996-07-04t00:00:00" productid= "a"/>

Auto Sample
We execute the following SQL statement:
' The result is limited to 3 records.
SET ROWCOUNT 3
SELECT Orders.OrderID, Orders.orderdate, ProductID
From Orders, [order Details]
WHERE Orders.OrderID = [Order Details]. OrderID
ORDER BY Orders.OrderID
For XML AUTO

The resulting results are as follows:
<orders orderid= "10248" orderdate= "1996-07-04t00:00:00" >
<order_x0020_details productid= "one"/>
<order_x0020_details productid= "/>"
<order_x0020_details productid= "/>"
</Orders>

Explicit sample
The explicit mode gives the query programmer complete control over the resulting XML. But this control is expensive: you have to write each query so that SQL statements can contain XML information.

The syntax is complex and beyond the scope of this article. [, XMLDATA] [, ELEMENTS] [, BINARY BASE64] is the appropriate optional parameter.






Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.