Hello everyone,
I have run into a couple of problems that are baffling me. I am able to select XML data if the variable @MyXML is populated using text and a SET command, but if I populate @MyXML using a select statement it will not allow me to parse the variable and return results.
For Example:
Works really well, but similar logic that populates the variables differently will not work, for example:
Returns no data and no errors.
Any thoughts? Stuck on this item.
Thanks,
Patrick
I have run into a couple of problems that are baffling me. I am able to select XML data if the variable @MyXML is populated using text and a SET command, but if I populate @MyXML using a select statement it will not allow me to parse the variable and return results.
For Example:
Code:
DECLARE
@MySql VARCHAR(2000)
SET
@MySql='
<OptionList>
<Option isCorrect="1" Shuffle="0" WeightPercent="100">
<text ident="1">A.Trader:Market</text>
<feedback></feedback>
</Option>
<Option isCorrect="0" Shuffle="0" WeightPercent="0">
<text ident="2">B.Doctor:Patent</text>
<feedback></feedback>
</Option>
<Option isCorrect="1" Shuffle="0" WeightPercent="100">
<text ident="3">C.Teacher:School</text>
<feedback></feedback>
</Option>
<Option isCorrect="0" Shuffle="0" WeightPercent="0">
<text ident="4">D.Athelete:Olympics</text>
<feedback></feedback>
</Option>
</OptionList>'
DECLARE
@MyXml XML
SET
@MyXml = CAST(@MySql AS XML)
SELECT
Question.Answer.value('(text/@ident)[1]', 'VARCHAR(50)') AS Ident,
Question.Answer.value('text[1]', 'VARCHAR(50)') AS Name,
Question.Answer.value('@WeightPercent', 'INT') AS WeightPercent,
Question.Answer.value('@Shuffle', 'INT') AS Shuffle,
Question.Answer.value('@isCorrect', 'INT') AS IsCorrect
FROM
@MyXml.nodes('/OptionList/Option') AS Question(Answer)
Works really well, but similar logic that populates the variables differently will not work, for example:
Code:
DECLARE
@MySql VARCHAR(2000)
set @MySql = (select OptionData from QA_Question where Question_PK = 1)
DECLARE @MyXml XML ;
SELECT
Question.Answer.value('(text/@ident)[1]', 'VARCHAR(50)') AS Ident,
Question.Answer.value('text[1]', 'VARCHAR(50)') AS Name,
Question.Answer.value('@WeightPercent', 'INT') AS WeightPercent,
Question.Answer.value('@Shuffle', 'INT') AS Shuffle,
Question.Answer.value('@isCorrect', 'INT') AS IsCorrect
FROM
@MyXml.nodes('/OptionList/Option') AS Question(Answer)
Returns no data and no errors.
Any thoughts? Stuck on this item.
Thanks,
Patrick