2012-08-07 62 views
0

我想從存儲過程中的xml中獲取數據,但它會爲每個記錄返回null請幫助!用於存儲過程的XML輸入

下面是我的腳本我在存儲過程中使用獲取記錄:

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 
+0

您檢索的所有值都位於結果/ DiaChannelDataHistoryFull下的節點中。因此,我希望你應該從@ deviceData.nodes('/ result/DiaChannelDataHistoryFull/id')指定tbl(clms) – DeanOC 2012-08-07 23:00:22

+0

不,一些值來自和其他來自本身,如dcdStringValue ... – ANKIT 2012-08-07 23:04:29

回答

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) 
+0

Mikael,它的工作原理.. 謝謝! – ANKIT 2012-08-08 20:54:47