Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQLServer2008中读取XML_Xml_Sql Server 2008 - Fatal编程技术网

在SQLServer2008中读取XML

在SQLServer2008中读取XML,xml,sql-server-2008,Xml,Sql Server 2008,我想从下面的XML中读取ContactId和注释。我尝试使用Openxml并声明名称空间,但没有得到预期的结果。请您提供解决方案 <ArrayOfContact xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Contact> <ContactId xmlns="http://www.thomsonreuters.

我想从下面的XML中读取ContactId和注释。我尝试使用Openxml并声明名称空间,但没有得到预期的结果。请您提供解决方案

<ArrayOfContact xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Contact>
<ContactId xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">100</ContactId>
<Notes xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">Test1</Notes>
</Contact>
<Contact>
<ContactId xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">101</ContactId>
<Notes xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">Test2</Notes>
</Contact>
</ArrayOfContact>

100
测试1
101
测试2

OPENXML函数是SQL Server 2000的传统XML支持,我会使用
XML
数据类型中引入的更新的XML方法

DECLARE @x xml;
SET @x = '<ArrayOfContact xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Contact>
<ContactId xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">100</ContactId>
<Notes xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">Test1</Notes>
</Contact>
<Contact>
<ContactId xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">101</ContactId>
<Notes xmlns="http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData">Test2</Notes>
</Contact>
</ArrayOfContact>';

WITH XMLNAMESPACES ('http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData' AS cms)
SELECT
    contact.value('cms:ContactId[1]', 'int') ContactId, 
    contact.value('cms:Notes[1]', 'nvarchar(MAX)') Notes 
    FROM @x.nodes('/ArrayOfContact/Contact') AS contacts(contact);
DECLARE@xxml;
设置@x='1
100
测试1
101
测试2
';
使用XMLNAMESPACES('http://www.thomsonreuters.com/2011/02/25/CMS/CmsService/ContactData“作为cms)
挑选
contact.value('cms:ContactId[1],'int')ContactId,
contact.value('cms:Notes[1],'nvarchar(MAX)'注释
从@x.nodes('/ArrayOfContact/Contact')作为联系人(Contact);