存储过程的XML输入

时间:2012-08-07 22:52:00

标签: sql sql-server-2008

我想从存储过程中的xml中获取数据,但是每个记录都返回null请帮助!!

以下是我在存储过程中用于获取记录的脚本:

declare @deviceData xml
set @deviceData = 
'<result>
   <resultTotalRows>24</resultTotalRows>
   <requestedStartRow>0</requestedStartRow>
   <resultSize>24</resultSize>
   <requestedSize>1000</requestedSize>
   <remainingSize>0</remainingSize>
   <DiaChannelDataHistoryFull>
      <id>
         <devConnectwareId>00000000-00000000-00409DFF-FF521221</devConnectwareId>
         <ddInstanceName>bpcuff1</ddInstanceName>
         <dcChannelName>diastolic</dcChannelName>
         <dcdhId>549747901</dcdhId>
      </id>
      <cstId>6423</cstId>
      <xpExtAddr>00:13:A2:00:40:6B:95:43</xpExtAddr>
      <dcDataType>0</dcDataType>
      <dcdUpdateTime>2012-07-31T20:53:44.000Z</dcdUpdateTime>
      <dcdStringValue>82</dcdStringValue>
      <dcdIntegerValue>82</dcdIntegerValue>
   </DiaChannelDataHistoryFull>
</result>'

declare @xmltable table (PatientID int, GatewayId nvarchar(255), DeviceName nvarchar(255), TimeStamp datetime, Systolic int, Diastolic int, Pulse int, Status nvarchar(255))
insert into @xmltable (PatientID, GatewayId, DeviceName, TimeStamp, Systolic, Diastolic, Pulse, Status)
select
1 as PatientID,
tbl.clms.value('@devConnectwareId','nvarchar(255)') as GatewayId,
tbl.clms.value('@ddInstanceName','nvarchar(255)') as DeviceName,
tbl.clms.value('@dcdUpdateTime','datetime') as TimeStamp,
case tbl.clms.value('@dcChannelName','nvarchar(255)') when 'Systolic' then tbl.clms.value('@dcdStringValue','nvarchar(255)') else null end as Systolic,
case tbl.clms.value('@dcChannelName','nvarchar(255)') when 'Diastolic' then tbl.clms.value('@dcdStringValue','nvarchar(255)') else null end as Diastolic,
case tbl.clms.value('@dcChannelName','nvarchar(255)') when 'Pulse' then tbl.clms.value('@dcdStringValue','nvarchar(255)') else null end as Pulse,
case tbl.clms.value('@dcChannelName','nvarchar(255)') when 'Sample_Status' then tbl.clms.value('@dcdStringValue','nvarchar(255)') else null end as Status
from @deviceData.nodes('/result/DiaChannelDataHistoryFull') tbl(clms)

select * from @xmltable

1 个答案:

答案 0 :(得分:0)

使用@用于属性而不是节点值,您必须为id节点提供其中值的路径。

select
1 as PatientID,        
tbl.clms.value('(id/devConnectwareId)[1]','nvarchar(255)') as GatewayId,
tbl.clms.value('(id/ddInstanceName)[1]','nvarchar(255)') as DeviceName,
tbl.clms.value('dcdUpdateTime[1]','datetime') as TimeStamp,
case tbl.clms.value('(id/dcChannelName)[1]','nvarchar(255)') when 'Systolic' then tbl.clms.value('dcdStringValue[1]','nvarchar(255)') else null end as Systolic,
case tbl.clms.value('(id/dcChannelName)[1]','nvarchar(255)') when 'Diastolic' then tbl.clms.value('dcdStringValue[1]','nvarchar(255)') else null end as Diastolic,
case tbl.clms.value('(id/dcChannelName)[1]','nvarchar(255)') when 'Pulse' then tbl.clms.value('dcdStringValue[1]','nvarchar(255)') else null end as Pulse,
case tbl.clms.value('(id/dcChannelName)[1]','nvarchar(255)') when 'Sample_Status' then tbl.clms.value('dcdStringValue[1]','nvarchar(255)') else null end as Status
from @deviceData.nodes('/result/DiaChannelDataHistoryFull') tbl(clms)