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 ))