2013-01-24 237 views
2
<Findings> 
    <Finding EcinRecordID="1042893"> 
     <Name>Goal Length of Stay for the ORG</Name> 
     <Selected Value="0" DisplayValue="No"/> 
    </Finding> 
    <Finding EcinRecordID="1042894"> 
     <Name>Goal Length of Stay for the GRG</Name> 
     <Selected Value="1" DisplayValue="Yes"/> 
     <NoteText>3 days</NoteText> 
    </Finding> 
</Findings> 

2個挑戰:調查結果基於兄弟節點SQL選擇XML節點屬性值

  1. 選擇節點值/查找/名在發現/查找/選定值= 「1」
  2. 選擇節點其中Findings/Finding/Selected Value =「1」的Findings/Finding/NoteText的值

將其放入存儲過程。我已經嘗試了至少三十個使用查詢,存在和值的版本。我可以得到所選值是否爲'1',但似乎無法在Select語句中分配相應的Name值。

SELECT 
    p.value('(Payments[1]/Payment[1]/PreAuthCertNumber)[1]', 'varchar(20)') AS PriorAuthNumber 
    ,qa.value('(Name[1])','varchar(255)') AS Question 
    ,qa.value('(Findings/Finding/Name)[1]','varchar(255)') AS Answer 
    FROM #ValueExample 
    CROSS APPLY XMLDocument.nodes('/OutboundDataFeed/Patient/PatientAdmission') as t(p) 
    CROSS APPLY XMLDocument.nodes('/OutboundDataFeed/Patient/PatientAdmission/CMAssessments/CMAssessment/Sections/Section/Questions/Question') as u(qa) 

謝謝!

回答

6
declare @XML xml 

set @XML = ' 
<Findings> 
    <Finding EcinRecordID="1042893"> 
     <Name>Goal Length of Stay for the ORG</Name> 
     <Selected Value="0" DisplayValue="No"/> 
    </Finding> 
    <Finding EcinRecordID="1042894"> 
     <Name>Goal Length of Stay for the GRG</Name> 
     <Selected Value="1" DisplayValue="Yes"/> 
     <NoteText>3 days</NoteText> 
    </Finding> 
</Findings>' 

select @XML.value('(/Findings/Finding[Selected/@Value = "1"]/Name/text())[1]', 'varchar(255)') as Name, 
     @XML.value('(/Findings/Finding[Selected/@Value = "1"]/NoteText/text())[1]', 'varchar(255)') as NoteText 

結果:

Name          NoteText 
---------------------------------------- ------------------------- 
Goal Length of Stay for the GRG   3 days 
+0

太棒了!非常感謝! – user2005792

2

僅供參考,當我去嘗試實現在與我的工作XML字段的查詢這個解決方案,我發現了Mikael埃裏克森的回答提供的語法上面沒有爲我工作。結構是正確的,我仔細檢查了它是完好的,但我只是得到了結果的空值。

所提供(來自的Mikael埃裏克森的回答剪斷)是語法:

select @XML.value('(/Findings/Finding[Selected/@Value = "1"]/Name/text())[1]', 'varchar(255)') as Name, 
     @XML.value('(/Findings/Finding[Selected/@Value = "1"]/NoteText/text())[1]', 'varchar(255)') as NoteText 

我發現什麼是真正爲我所做的工作如下(顯然稍微修改它,這不是我自己的代碼片段,但出於一致性的目的):

select @XML.value('(/Findings/Finding/Selected[@Value = "1"]/Name/text())[1]', 'varchar(255)') as Name, 
     @XML.value('(/Findings/Finding/Selected[@Value = "1"]/NoteText/text())[1]', 'varchar(255)') as NoteText 

只是以爲我會發布爲別人誰可能會遇到和我一樣的經歷備用。

+0

謝謝@wildwend,我遇到了和你一樣的問題。 – cBlaine