Sqlserver: Read configuration information from XML files

Source: Internet
Author: User

How can I read configuration information from external XML files in sqlserver? This problem is caused by a company's written test information.

XML file content:

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<DB name = "clientdb1" datasize = "512 MB" Maid = "100 MB" logsize = "100 MB" loggrowth = "50 MB">
</DB>
<DB name = "clientdb2" datasize = "1536 MB" Maid = "200 MB" logsize = "100 MB" loggrowth = "100 MB">
</DB>
<DB name = "clientdbn" datasize = "1024 MB" Maid = "200 MB" logsize = "100 MB" loggrowth = "80 Mb">
</DB>
</Root>

 

 

Script:

Declare @ x XML
Declare @ hdocid int


/* Read the file content from an external file

There are many methods:

Xp_cmdshell calls the Extended Stored Procedure of the explicit text file content of the operating system.

Bcp.exe import and export tool

OpenRowSet reads text file content (sql2005 is enhanced)

Bulk insert statement

*/

Select @ x = bulkcolumn
From OpenRowSet (bulk 'C: \ db_conifig.xml ', single_clob) as t -- Enhanced bulk Syntax of sqlserver2005

 

-- Parse to XML tree document in memory

Exec sp_xml_preparedocument @ hdocid output, @ x

-- SQL Server is parsed into a result set for ease of use. Or you can directly use the XML-type method for parsing!

Select *
From openxml (@ hdocid, '/root/db') with (name varchar (50), datasize varchar (50), then rowth varchar (50), logsize varchar (50 ), loggrowth varchar (50 ))

 

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.