查找XML中的所有名字

查找XML中的所有名字,xml,tsql,Xml,Tsql,名为AdvantureWorks2012的Microsoft practice数据库有一个名为HumanResources.JobCandidate的表。那张表有几列。其中一列是uri数据类型。这是该列中一个单元格内的内容: <ns:Resume xmlns:ns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume"> <ns:Name> <ns:Name.Pref

名为AdvantureWorks2012的Microsoft practice数据库有一个名为HumanResources.JobCandidate的表。那张表有几列。其中一列是uri数据类型。这是该列中一个单元格内的内容:

<ns:Resume xmlns:ns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume">
  <ns:Name>
    <ns:Name.Prefix></ns:Name.Prefix>
    <ns:Name.First>Shai</ns:Name.First>
    <ns:Name.Middle></ns:Name.Middle>
    <ns:Name.Last>Bassli</ns:Name.Last>
    <ns:Name.Suffix></ns:Name.Suffix>
  </ns:Name>
  <ns:Skills>
I am an experienced and versatile machinist who can operate a range of machinery personally as well as supervise the work of other machinists. I specialize in diagnostics and precision inspection, have expertise in reading blueprints, and am able to call on strong interpersonal and communication skills to guide the work of other production machinists whose work I am called upon to inspect. 
My degree in mechanical engineering affords me a better theoretical understanding and mathematical background than many other candidates in the machinist trade.
    </ns:Skills>
  <ns:Employment>
    <ns:Emp.StartDate>2000-06-01Z</ns:Emp.StartDate>
    <ns:Emp.EndDate>2002-09-30Z</ns:Emp.EndDate>
    <ns:Emp.OrgName>Wingtip Toys</ns:Emp.OrgName>
    <ns:Emp.JobTitle>Lead Machinist</ns:Emp.JobTitle>
    <ns:Emp.Responsibility> Supervised work of staff of four machinists. Coordinated all complex assembly and tooling activities, including production of tricycles and wagons.
Developed parts fabrication from sample parts, drawings and verbal orders.Worked with ISO9000 implementation.
        </ns:Emp.Responsibility>
    <ns:Emp.FunctionCategory>Production</ns:Emp.FunctionCategory>
    <ns:Emp.IndustryCategory>Manufacturing</ns:Emp.IndustryCategory>
    <ns:Emp.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:Emp.Location>
  </ns:Employment>
  <ns:Employment>
    <ns:Emp.StartDate>1996-11-15Z</ns:Emp.StartDate>
    <ns:Emp.EndDate>2000-05-01Z</ns:Emp.EndDate>
    <ns:Emp.OrgName>Blue Yonder Airlines</ns:Emp.OrgName>
    <ns:Emp.JobTitle>Machinist</ns:Emp.JobTitle>
    <ns:Emp.Responsibility>Repaired and maintained a variety of production and fabrication machine tools.
Set up and operated machines to close tolerances. Used and wrote CNC machine programs. Trained extensively in computer-aided manufacturing.
        </ns:Emp.Responsibility>
    <ns:Emp.FunctionCategory>Production</ns:Emp.FunctionCategory>
    <ns:Emp.IndustryCategory>Manufacturing</ns:Emp.IndustryCategory>
    <ns:Emp.Location>
      <ns:Location>
        <ns:Loc.CountryRegion>US </ns:Loc.CountryRegion>
        <ns:Loc.State>IL </ns:Loc.State>
        <ns:Loc.City>Chicago</ns:Loc.City>
      </ns:Location>
    </ns:Emp.Location>
  </ns:Employment>
  <ns:Employment>
    <ns:Emp.StartDate>1994-06-10Z</ns:Emp.StartDate>
    <ns:Emp.EndDate>1996-07-22Z</ns:Emp.EndDate>
    <ns:Emp.OrgName>City Power and Light</ns:Emp.OrgName>
    <ns:Emp.JobTitle>Assistant Machinist</ns:Emp.JobTitle>
    <ns:Emp.Responsibility>Performed centerless grinding. Received training in manual mill and lathe machines, as well as micrometers and calipers.
Owned complete toolset.Worked extensive overtime on request. </ns:Emp.Responsibility>
    <ns:Emp.FunctionCategory>Production</ns:Emp.FunctionCategory>
    <ns:Emp.IndustryCategory>Manufacturing</ns:Emp.IndustryCategory>
    <ns:Emp.Location>
      <ns:Location>
        <ns:Loc.CountryRegion>US </ns:Loc.CountryRegion>
        <ns:Loc.State>IA </ns:Loc.State>
        <ns:Loc.City>Des Moines</ns:Loc.City>
      </ns:Location>
    </ns:Emp.Location>
  </ns:Employment>
  <ns:Education>
    <ns:Edu.Level>Bachelor</ns:Edu.Level>
    <ns:Edu.StartDate>1990-09-15Z</ns:Edu.StartDate>
    <ns:Edu.EndDate>1994-05-10Z</ns:Edu.EndDate>
    <ns:Edu.Degree>Bachelor of Science</ns:Edu.Degree>
    <ns:Edu.Major>Mechanical Engineering</ns:Edu.Major>
    <ns:Edu.Minor></ns:Edu.Minor>
    <ns:Edu.GPA>3.2</ns:Edu.GPA>
    <ns:Edu.GPAScale>4</ns:Edu.GPAScale>
    <ns:Edu.School>Midwest State University</ns:Edu.School>
    <ns:Edu.Location>
      <ns:Location>
        <ns:Loc.CountryRegion>US </ns:Loc.CountryRegion>
        <ns:Loc.State>IA </ns:Loc.State>
        <ns:Loc.City>Ames</ns:Loc.City>
      </ns:Location>
    </ns:Edu.Location>
  </ns:Education>
  <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:Addr.Telephone>
      <ns:Telephone>
        <ns:Tel.Type>Voice</ns:Tel.Type>
        <ns:Tel.IntlCode>1</ns:Tel.IntlCode>
        <ns:Tel.AreaCode>276</ns:Tel.AreaCode>
        <ns:Tel.Number>555-0114</ns:Tel.Number>
      </ns:Telephone>
      <ns:Telephone>
        <ns:Tel.Type>Fax</ns:Tel.Type>
        <ns:Tel.IntlCode>1</ns:Tel.IntlCode>
        <ns:Tel.AreaCode>276</ns:Tel.AreaCode>
        <ns:Tel.Number>555-0132</ns:Tel.Number>
      </ns:Telephone>
    </ns:Addr.Telephone>
  </ns:Address>
  <ns:EMail>Shai@Example.com</ns:EMail>
  <ns:WebSite></ns:WebSite>
</ns:Resume>

谢
巴斯利
我是一名经验丰富、多才多艺的机械师,可以亲自操作一系列机械,也可以监督其他机械师的工作。我擅长诊断和精密检验,有阅读蓝图的专业知识,能够运用强大的人际关系和沟通技能指导其他生产机械师的工作,我被要求检查他们的工作。
我的机械工程学位使我比机械行业的许多其他候选人有更好的理论理解和数学背景。
2000-06-01Z
2002-09-30Z
翼尖玩具
首席机械师
监督四名机械师员工的工作。协调所有复杂的装配和工装活动,包括三轮车和货车的生产。
根据样品零件、图纸和口头订单开发零件制造。配合ISO9000的实施。
生产
制造业
美国
医疗保险
萨吉诺
1996-11-15Z
2000-05-01Z
蓝色那边航空公司
机械师
维修和保养各种生产和制造机床。
设置和操作机器,使其接近公差。使用并编写数控机床程序。在计算机辅助制造方面受过广泛培训。
生产
制造业
美国
白细胞介素
芝加哥
1994-06-10Z
1996-07-22Z
城市电力与照明
助理机械师
进行无心磨削。接受过手动铣床和车床以及千分尺和卡尺的培训。
拥有完整的工具集。应要求加班。
生产
制造业
美国
IA
得梅因
单身汉
1990-09-15Z
1994-05-10Z
理学士
机械工程
3.2
4.
中西部州立大学
美国
IA
艾姆斯
家
第三大道567号
美国
医疗保险
萨吉诺
53900
声音
1.
276
555-0114
传真
1.
276
555-0132
Shai@Example.com

我需要在本专栏中找到起诉tsql的所有名字和姓氏。我只是一个初学者,所以一个详细的解释会有很大帮助。谢谢。

第一步,注册默认名称空间URI(XML的这部分:
xmlns=“…”
):

然后,您可以对传递XQuery作为第一个参数和传递SQL数据类型作为第二个参数的XML列使用
value()
方法,例如:

Resume.value('(/Resume/Name/Name.First)[1]', 'varchar(100)') AS 'First'
以上XQuery/Xpath分解:

  • (…)[1]
    :获取内部XQuery/XPath(
    部分)的第一个结果
  • /Resume/Name/Name.First
    :在本例中,使用的XQuery/XPath只是一个简单的路径表达式,就像我们电脑中的文件/文件夹路径一样。但是,与表示存储中的文件/文件夹位置不同,这个XPath/XQuery表示整个XML文档中的XML节点位置
完整的查询示例如下所示:

WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume')

select 
  Resume.value('(/Resume/Name/Name.First)[1]', 'varchar(100)') AS 'First',
  Resume.value('(/Resume/Name/Name.Last)[1]', 'varchar(100)') AS 'Last'
from HumanResources.JobCandidate
供参考:


使用TSQL还是什么???@Oscar抱歉,说得好。我正在使用tsql。问题解决了。
WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume')

select 
  Resume.value('(/Resume/Name/Name.First)[1]', 'varchar(100)') AS 'First',
  Resume.value('(/Resume/Name/Name.Last)[1]', 'varchar(100)') AS 'Last'
from HumanResources.JobCandidate