Export data from a table in SQL Server to an XML file

Source: Internet
Author: User
How to export data from a table in SQL Server to an XML file, use the SQL language, and implement it automatically? The best answer: Select * from table name for XML: The for clause is used to specify browse or XML options (browse and XML are irrelevant options ). Syntax [For {browse | XML {raw | auto | explicit} [, xmldata] [, elements] [, binary base64]}] The Browse parameter specifies that data can be updated when you view data in the DB-library browser mode cursor. If the table contains a timestamp column (a column defined by the timestamp data type), the table has a unique index and the for browse option is sent to SQL server at the end of the SELECT statement Program . <Lock_hint> holdlock cannot be used in select statements containing the for browse option. The for browse option cannot appear in the SELECT statement joined by the Union operator. XML indicates that the query result will be returned as an XML document. You must specify one of the following XML modes: raw, auto, and explicit. Raw obtains the query results and converts the rows in the result set to XML elements. The general identifier <row/> is used as the element tag. Test: Select Top 2 book no. = bookno, book name = bookname from sys_books as book information for XML raw result: <row book no. = "b001" book name = "1"/> <row book no. = "B002" book name = "(Shanghai) "/> auto returns the query result with a simple Nested XML tree. In the from clause, each table listed in at least one column in the select clause is represented as an XML element. Columns listed in the select clause map to appropriate element features. Test: Select Top 2 book no. = bookno, book name = bookname from sys_books as book information for XML auto result: <book info book no. = "b001" book name = "1"/> <book info book no. = "B002" book name = "(Shanghai) "/> explicit specifies the shape of the XML tree explicitly defined. In this mode, you need to write a query in a specific way to explicitly specify additional information about the expected nesting. Xmldata returns the schema, but does not add the root element to the result. If xmldata is specified, it is appended to the document. Elements specifies the column to be returned as a child element. Otherwise, columns are mapped to the XML feature. Test: Select Top 2 book no. = bookno, book name = bookname from sys_books as book information for XML auto, elements result: <book info> <book no.> b001 </book no.> <book name> 1 </book name> </book info> <book no.> B002 </ book No.> <book name> Sports News (Shanghai) </book name> </book information> nary base64 indicates that binary data in the binary base64 encoding format is returned for query. This option must be specified when binary data is retrieved in raw and explicit modes. This is the default value in auto mode.

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.