1

我在 SQL 服务器中有一个 XML 类型列:

<LogMessage xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="LogMessageBusinessServiceRequest">
  <Application>Services.Business.myapp</Application>
  <Time>2013-05-30T15:01:38.932Z</Time>
  <Level>Info</Level>
  <Message>MultiQuery Biz Request</Message>
  <MachineName>Machine1</MachineName>
  <ThreadId>16084</ThreadId>
  <Callsite>BLAH</Callsite>
  <CreatedBy>Machine1\svc_biz_myapp</CreatedBy>
  <Context>
    <myappExtraInfo xmlns="http://services.somedomain.com/myapp/logging/extraInfo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <CallType>MultiProcessQuery</CallType>
      <RequestId>r163505508498822282742</RequestId>
      <FirmId>160</FirmId>
      <PMFirmId>203</PMFirmId>
      <SubscriptionId>0</SubscriptionId>
      <Token />
      <LastClientSvcTimeMs>0</LastClientSvcTimeMs>
      <ResultCode>0</ResultCode>
      <ResultCodeDescription>OK</ResultCodeDescription>
      <ElapsedTimeTotalMs>110</ElapsedTimeTotalMs>
      <ElapsedTimeMtMs>110</ElapsedTimeMtMs>
      <ElapsedTimeDacMs>109</ElapsedTimeDacMs>
      <ElapsedTimePMSSMs>-1</ElapsedTimePMSSMs>
    </myappExtraInfo>
  </Context>

我想从那里获取 FirmID。但我无法使用我的选择语句的以下部分:

select
[Body].value('(/LogMessage/Context/myappExtraInfo/FirmId)[1]', 'varchar(max)') FirmID

我究竟做错了什么?

谢谢你的时间。

  • 什亚姆
4

1 回答 1

0

试试这个 - 你需要尊重在你的节点上定义的 XML 命名空间<myappExtraInfo>(因此也适用于所有子节点):

DECLARE @Test TABLE (ID INT NOT NULL, XmlContent XML)

INSERT INTO @Test VALUES(1, 
'<LogMessage xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="LogMessageBusinessServiceRequest">
  <Application>Services.Business.myapp</Application>
  <Time>2013-05-30T15:01:38.932Z</Time>
  <Level>Info</Level>
  <Message>MultiQuery Biz Request</Message>
  <MachineName>Machine1</MachineName>
  <ThreadId>16084</ThreadId>
  <Callsite>BLAH</Callsite>
  <CreatedBy>Machine1\svc_biz_myapp</CreatedBy>
  <Context>
    <myappExtraInfo xmlns="http://services.somedomain.com/myapp/logging/extraInfo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <CallType>MultiProcessQuery</CallType>
      <RequestId>r163505508498822282742</RequestId>
      <FirmId>160</FirmId>
      <PMFirmId>203</PMFirmId>
      <SubscriptionId>0</SubscriptionId>
      <Token />
      <LastClientSvcTimeMs>0</LastClientSvcTimeMs>
      <ResultCode>0</ResultCode>
      <ResultCodeDescription>OK</ResultCodeDescription>
      <ElapsedTimeTotalMs>110</ElapsedTimeTotalMs>
      <ElapsedTimeMtMs>110</ElapsedTimeMtMs>
      <ElapsedTimeDacMs>109</ElapsedTimeDacMs>
      <ElapsedTimePMSSMs>-1</ElapsedTimePMSSMs>
    </myappExtraInfo>
  </Context>
 </LogMessage>')


;WITH XMLNAMESPACES('http://services.somedomain.com/myapp/logging/extraInfo' AS ns)
SELECT
    XmlContent.value('(/LogMessage/Context/ns:myappExtraInfo/ns:FirmId)[1]', 'int')
FROM @Test
WHERE ID = 1  

这会将值返回160给我。

于 2013-05-31T15:06:43.160 回答