2017-05-04 7 views
1

데이터라는 varchar (max) 열이있는 Content라는 테이블이 있습니다.varchar (max) 열의 SQL 쿼리 XML 특성

이 열의 데이터는 XML 형식으로되어 있습니다 (데이터베이스를 설계하지 않았으므로이 열이 XML 형식이 아닌 이유를 알지 못했습니다. 몇 가지 다른 XML 열이 있습니다. 동일한 데이터베이스 ... go figure ...), XML의 속성을 쿼리하려고합니다.

아래 XML을 복사했습니다. 쿼리 할 내용은 p3 : productId 값입니다. 현재 내가 가지고있는 질문은 다음과 같습니다 (여기에서 다른 질문을 통해 질문했습니다).

WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2001/XMLSchema-instance' 
          ,'http://www.w3.org/2001/XMLSchema-instance' AS p3) 
SELECT CAST(Data AS XML).value(N'(/manifest/p3:productList/product/productId)[1]',N'nvarchar(max)') 
FROM Content 

그러나 이것은 모든 행에 대해 NULL로 돌아옵니다.

확실히 비슷하지만 확실히 내 머리를 얻을 수는 없지만 XML을 쿼리하는 데 많은 경험이 없습니다 (분명히!).

<manifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://www.w3.org/2001/XMLSchema-instance" 
     p3:manifestDateTime="2016-02-17T17:34:29.5925096Z" 
     p3:manifestVersion="1.1" p3:manifestId="Some_Data"> 
    <p3:productList> 
    <p3:product p3:releaseDateTime="2016-02-17T17:34:29.5925096Z"   
    p3:installSeqId="2" p3:uninstallSeqId="2" p3:releaseNum="1" 
    p3:productType="doc" p3:productId="WEDREZ020RRRP0GGG001" p3:mfgCode="GIRE"> 

답변

1

다음 코드는 (닫는 태그를 추가해야하는) xml의 각 값을 추출합니다. 당신의 <p3:productList>에서 다양한 <p3:product> 요소가있는 경우 .nodes() 호출은 동일한 쿼리를 허용합니다 :

DECLARE @xml XML= 
'<manifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://www.w3.org/2001/XMLSchema-instance" 
      p3:manifestDateTime="2016-02-17T17:34:29.5925096Z" 
      p3:manifestVersion="1.1" 
      p3:manifestId="Some_Data"> 
    <p3:productList> 
    <p3:product p3:releaseDateTime="2016-02-17T17:34:29.5925096Z" 
       p3:installSeqId="2" 
       p3:uninstallSeqId="2" 
       p3:releaseNum="1" 
       p3:productType="doc" 
       p3:productId="WEDREZ020RRRP0GGG001" 
       p3:mfgCode="GIRE" /> 
    </p3:productList> 
</manifest>'; 

--Thq 쿼리

WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2001/XMLSchema-instance' 
          ,'http://www.w3.org/2001/XMLSchema-instance' AS p3) 
SELECT @xml.value(N'(/manifest/@p3:manifestDateTime)[1]',N'datetime') AS manifestDateTime 
     ,@xml.value(N'(/manifest/@p3:manifestVersion)[1]',N'nvarchar(max)') AS manifestVersion 
     ,@xml.value(N'(/manifest/@p3:manifestId)[1]',N'nvarchar(max)') AS manifestId 
     ,p.value(N'@p3:releaseDateTime',N'datetime') AS Product_releaseDateTime 
     ,p.value(N'@p3:installSeqId',N'int') AS Product_installSeqId 
     ,p.value(N'@p3:uninstallSeqId',N'int') AS Product_uninstallSeqId 
     ,p.value(N'@p3:releaseNum',N'int') AS Product_releaseNum 
     ,p.value(N'@p3:productType',N'nvarchar(max)') AS Product_productType 
     ,p.value(N'@p3:productId',N'nvarchar(max)') AS Product_productId 
     ,p.value(N'@p3:mfgCode',N'nvarchar(max)') AS Product_mfgCode 
FROM @xml.nodes(N'/manifest/p3:productList/p3:product') AS A(p); 

결과는

+-------------------------+-----------------+------------+-------------------------+----------------------+------------------------+--------------------+---------------------+----------------------+-----------------+ 
| manifestDateTime  | manifestVersion | manifestId | Product_releaseDateTime | Product_installSeqId | Product_uninstallSeqId | Product_releaseNum | Product_productType | Product_productId | Product_mfgCode | 
+-------------------------+-----------------+------------+-------------------------+----------------------+------------------------+--------------------+---------------------+----------------------+-----------------+ 
| 2016-02-17 17:34:29.593 | 1.1    | Some_Data | 2016-02-17 17:34:29.593 | 2     | 2      | 1     | doc     | WEDREZ020RRRP0GGG001 | GIRE   | 
+-------------------------+-----------------+------------+-------------------------+----------------------+------------------------+--------------------+---------------------+----------------------+-----------------+ 
+0

굉장이 작품 간식. 전설 : o) –