我在SQL表中的下列XML:查詢檢索的XML值在SQL
<SiteSurveyData>
<SurveyQuestionAnswers>
<QuestionGUID>61</QuestionGUID>
<Answer>XXXXXXX</Answer>
</SurveyQuestionAnswers>
<SurveyQuestionAnswers>
<QuestionGUID>62</QuestionGUID>
<Answer>XXX</Answer>
</SurveyQuestionAnswers>
<SurveyQuestionAnswers>
<QuestionGUID>64</QuestionGUID>
<Answer>Both</Answer>
</SurveyQuestionAnswers>
<SurveyQuestionAnswers>
<QuestionGUID>63</QuestionGUID>
<Answer>aal5snap</Answer>
</SurveyQuestionAnswers>
<SurveyQuestionAnswers>
<QuestionGUID>81</QuestionGUID>
<Answer>00</Answer>
</SurveyQuestionAnswers>
什麼我想查詢的是,我想在/應答的值,其中/ QuestionGUID = 61和62
我嘗試使用這樣的:
value('(/SiteSurveyData/SurveyQuestionAnswers/QuestionGUID)[1]'
,但它只是返回第一個QuestionGUID。
我如何編寫查詢以便它返回QuestionGUID 61和62的答案?
或者我只想爲QuestionGuid = 61得到答案。
它並不總是第一個元素,是否可以編寫查詢以便返回QuestionGuid = 61的值?
您可能需要使用遊標。這是一個示例https://stackoverflow.com/questions/19034382/sql-2012-iterate-through-an-xml-list-better-alternative-to-a-while-loop – Si8
如果我只想要所有的與問題= 61返回?它不一定總是第一個。 – user2665372