在SQLServer2008中读取XML
我想从下面的XML中读取ContactId和注释。我尝试使用Openxml并声明名称空间,但没有得到预期的结果。请您提供解决方案在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.
<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);