2011-07-14 2 views
0

以下のXMLからContactIdとNotesを読みたいと思います。 Openxmlを使って名前空間を宣言しようとしましたが、期待通りの結果が得られませんでした。ソリューションの提供を依頼してください。SQLサーバー2008でXMLを読む

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

答えて

2

OPENXML funcationは、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);