使用SQL和XML方法查询XML数据列

时间:2014-07-30 08:10:09

标签: sql-server xml xml-namespaces cross-apply

我正在对SQL Server 2012中的XML数据类型列执行查询。数据的一个示例是:

<ns:Resume xmlns:ns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume">
  <ns:Address>
    <ns:Addr.Type>Home</ns:Addr.Type>
    <ns:Addr.Street>567 3rd Ave</ns:Addr.Street>
    <ns:Addr.Location>
      <ns:Location>
        <ns:Loc.CountryRegion>US </ns:Loc.CountryRegion>
        <ns:Loc.State>MI </ns:Loc.State>
        <ns:Loc.City>Saginaw</ns:Loc.City>
      </ns:Location>
    </ns:Addr.Location>
    <ns:Addr.PostalCode>53900</ns:Addr.PostalCode>
  </ns:Address>
</ns:Resume>

我使用此link返回First姓氏,但现在我要返回所有来自芝加哥的候选人以及所有简历中找到的不同州。

对于来自芝加哥的所有候选人,我使用以下代码,但它总是返回列的名称,尽管该值存在。你能救我吗?

WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT 
JobCandidateID,
T.c.value('(ns:Name/ns:Name.First)[1]', 'nvarchar(100)') +N' '+
T.c.value('(ns:Name/ns:Name.Last)[1]', 'nvarchar(100)') as [First & Last Name],
T.c.value('(ns:Address/ns:Addr.Location/ns:Location/ns:Loc.City)[1]', 'nvarchar(100)') as [City Address],
T.c.value('(ns:Employment/ns:Emp.Location/ns:Location/ns:Loc.City)[1]', 'nvarchar(100)') as [City Employment]
FROM   
HumanResources.JobCandidate
CROSS APPLY
[Resume].nodes('/ns:Resume') AS T(c)
where [Resume].exist('/ns:Resume/ns:Address/ns:Addr.Location/ns:Location[ns:Loc.City="Chicago"]')=1;

1 个答案:

答案 0 :(得分:1)

来自芝加哥的所有候选人:

WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT 
JobCandidateID,
T.c.value('(ns:Name/ns:Name.First)[1]', 'nvarchar(100)') +N' '+
T.c.value('(ns:Name/ns:Name.Last)[1]', 'nvarchar(100)') as [First & Last Name],
T.c.value('(ns:Address/ns:Addr.Location/ns:Location/ns:Loc.City)[1]', 'nvarchar(100)') as [City Address],
T.c.value('(ns:Employment/ns:Emp.Location/ns:Location/ns:Loc.City)[1]', 'nvarchar(100)') as [City Employment]
FROM   
HumanResources.JobCandidate
CROSS APPLY
[Resume].nodes('/ns:Resume') AS T(c)
where [Resume].exist('/ns:Resume/ns:Employment/ns:Emp.Location/ns:Location[ns:Loc.City="Chicago"]')=1;

没有交叉申请:

WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT 
JobCandidateID,
Resume.value('(/ns:Resume/ns:Name/ns:Name.First)[1]', 'nvarchar(100)') +N' '+
Resume.value('(/ns:Resume/ns:Name/ns:Name.Last)[1]', 'nvarchar(100)') as [First & Last Name],
Resume.value('(/ns:Resume/ns:Address/ns:Addr.Location/ns:Location/ns:Loc.City)[1]', 'nvarchar(100)') as [City Address],
Resume.value('(/ns:Resume/ns:Employment/ns:Emp.Location/ns:Location/ns:Loc.City)[1]', 'nvarchar(100)') as [City Employment]
FROM   
HumanResources.JobCandidate
where Resume.exist('/ns:Resume/ns:Employment/ns:Emp.Location/ns:Location[ns:Loc.City="Chicago"]')=1;

对于所有简历中的不同状态:

WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT distinct
Resume.value('(/ns:Resume/ns:Address/ns:Addr.Location/ns:Location/ns:Loc.State)[1]', 'nvarchar(100)') as [State]
FROM   
HumanResources.JobCandidate