日期:2014-05-19  浏览次数:20654 次

请教各位大虾一个用sql语句读xml的问题?
假如说我读一个xml,位置在d:\abc\def.xml

xml的内容如下:

<company>
    <企业基本信息>
        <!--企业基本信息设定   -->
        <COMPANYINFO   code= "COMPANY_CODE "   value= "123456 "/>
        <COMPANYINFO   code= "COMPANY_NAME "   value= "xxx公司 "/>
        <COMPANYINFO   code= "COMPANY_LOGO "   value= "../../Contents/Img/logoxxx.jpg "/>
        <COMPANYINFO   code= "SUB_LOGO "   value= "../../Contents/Img/xxx.jpg "/>
        <COMPANYINFO   code= "ACC_NOTICE_USER "   value= "111111 "/>
        <COMPANYINFO   code= "STO_NOTICE_USER "   value= "222222 "/>
    </企业基本信息>
</company>

我如何在存贮过程中将 <COMPANYINFO   code= "ACC_NOTICE_USER "   value= "111111 "/>
后111111取出来???

谢谢了,openXML方法有点不太会用,所以来请教下各位csdn上的大虾们,希望大家不啬指教.

------解决方案--------------------
DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc = '
<company>
<baseinfo>
<COMPANYINFO code= "COMPANY_CODE " value= "123456 "/>
<COMPANYINFO code= "COMPANY_NAME " value= "xxxCompany "/>
<COMPANYINFO code= "COMPANY_LOGO " value= "../../Contents/Img/logoxxx.jpg "/>
<COMPANYINFO code= "SUB_LOGO " value= "../../Contents/Img/xxx.jpg "/>
<COMPANYINFO code= "ACC_NOTICE_USER " value= "111111 "/>
<COMPANYINFO code= "STO_NOTICE_USER " value= "222222 "/>
</baseinfo>
</company> '

--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT *
FROM OPENXML (@idoc, '/company/baseinfo/COMPANYINFO ',1)
WITH (code varchar(10),
value varchar(20))
------解决方案--------------------
DECLARE @x xml

SELECT @x = (
SELECT CONVERT(xml,T.c)
FROM OPENROWSET(BULK 'd:\abc\def.xml ',SINGLE_BLOB) T(c))

SELECT @x.value( '(/company/企业基本信息/COMPANYINFO[@code= "ACC_NOTICE_USER "]/@value)[1] ', 'varchar(max) ')