如何从SQL Server XML列查询值

时间:2015-08-21 06:54:38

标签: sql-server xml

我将XML存储在SQL Server数据库表的XML列中。

<MachineInfo>
  <OperatingSystem Caption="Microsoft Windows Server 2008 R2 Enterprise " InstallDate="2012-10-09T07:59:23Z" LastBootUpTime="2013-11-13T11:53:22Z" Manufacturer="Microsoft Corporation" OperatingSystemSKU="10" Organization="Organization" OSLanguage="1033" OSProductSuite="274" OSType="18" SerialNumber="00000-001-0000000-00000" ServicePackMajorVersion="1" ServicePackMinorVersion="0" Version="6.1.7601" WindowsDirectory="C:\Windows" />
  <ComputerSystem Caption="SERVERNAME" DNSHostName="servername" Domain="test.testdomain.com" DomainRole="4" Manufacturer="VMware, Inc." Model="VMware Virtual Platform" NumberOfLogicalProcessors="1" NumberOfProcessors="1" PartOfDomain="1" Roles="[ LM_Workstation ][ LM_Server ][ Backup_Domain_Controller ][ NT ][ Backup_Browser ][ DFS ]" SystemType="x64-based PC" TotalPhysicalMemory="4294500352" Workgroup="" />
  <Processors>
    <Processor AddressWidth="64" Architecture="9" Caption="Intel64 Family 6 Model 37 Stepping 1" DataWidth="64" ExtClock="" Family="2" L2CacheSize="0" L2CacheSpeed="" L3CacheSize="0" L3CacheSpeed="0" Manufacturer="GenuineIntel" MaxClockSpeed="3059" Name="Intel(R) Xeon(R) CPU           X5675  @ 3.07GHz" NumberOfCores="1" NumberOfLogicalProcessors="1" OtherFamilyDescription="" ProcessorId="0FABFBFF00020651" ProcessorType="3" Revision="9473" Stepping="" UniqueId="" Version="" VoltageCaps="2" />
  </Processors>
  <TimeZone Bias="0" Caption="(UTC) Dublin, Edinburgh, Lisbon, London" />
  <DiskDrives>
    <DiskDrive DeviceID="\\.\PHYSICALDRIVE0" InterfaceType="SCSI" Manufacturer="(Standard disk drives)" Model="VMware Virtual disk SCSI Disk Device" Partitions="2" Size="42944186880" />
    <DiskDrive DeviceID="\\.\PHYSICALDRIVE1" InterfaceType="SCSI" Manufacturer="(Standard disk drives)" Model="VMware Virtual disk SCSI Disk Device" Partitions="3" Size="42944186880" />
  </DiskDrives>
</MachineInfo>

我想获得ComputerSystem Caption的价值

表名:Agent 列名:MachineInfo

1 个答案:

答案 0 :(得分:1)

使用xml value method

SELECT 
    t.MachineInfo.value('(MachineInfo/ComputerSystem/@Caption)[1]','nvarchar(MAX)') AS Caption
FROM Agent t