我知道如何将XML变量加入到其他表中,但在这种情况下,我试图从表中选择每行加上来自每个对应表的XML 的结构排,旁边那行。我无法在线找到任何示例来帮助解决这个问题,因为大多数示例处理单个XML值(如果存在道歉,我无法在其他XML示例中找到它们)。除了结构化XML数据之外,还选择行数据
表结构是这样的:
CREATE TABLE tbl_QuizHistory (
HistoryId int PRIMARY KEY,
QuizData xml NOT NULL
);
每个QuizData
行值是与此类似:
<quizresult>
<question>
<questionText>Which fire extinguisher is most suitable for a waste paper basket fire?</questionText>
<answer number="0" value="0" chosen="0" imageURL="">Powder</answer>
<answer number="1" value="0" chosen="0" imageURL="">Carbon Dioxide (CO2)</answer>
<answer number="2" value="1" chosen="1" imageURL="">Water (H2O)</answer>
<answer number="3" value="0" chosen="0" imageURL="">Foam</answer>
<result>Correct</result>
</question>
<question>
<questionText>Should you use lifts during a fire?</questionText>
<answer number="0" value="0" chosen="0" imageURL="">Yes</answer>
<answer number="1" value="1" chosen="1" imageURL="">No</answer>
<result>Correct</result>
</question>
</quizresult>
在earlier question I was shown how to display the XML data hierarchically(@xml ==> questions ==> answer(s)
),但只为单个XML值,这是我适用于将问题/答案层级迁移到表格中:
-- Works for a single XML value/variable...
;WITH q AS (
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS qID,
n.q.value('(./questionText)[1]', 'nvarchar(max)') AS questionText,
n.q.value('(./result)[1]', 'nvarchar(50)') AS result,
n.q.query('answer') AS answers
FROM
@xml.nodes('/quizresult/question') AS n (q)
),
qa AS (
SELECT
qID,
questionText,
result,
answer.query('.') AS answer
FROM
q CROSS APPLY
answers.nodes('answer') AS a(answer)
)
SELECT
qa.qID,
q.questionText,
q.result,
qa.answer.value('answer[1]', 'nvarchar(max)') AS answer,
qa.answer.value('answer[1]/@number', 'int') AS number,
qa.answer.value('answer[1]/@value', 'int') AS val,
qa.answer.value('answer[1]/@chosen', 'bit') AS chosen
FROM
qa INNER JOIN
q ON qa.qID = q.qID;
如何将此逻辑应用于每个表格行中的每个XML值?我需要显示
- 测验HistoryId
- 每个问题从测验(为了清楚起见,可选的ID,虽然这是由SQL语句生成,并在XML不存在)
- 所有答案对每个问题
最终的结果我想实现会产生这样的:
HistoryId qID questionText result answer number val chosen
--------- ---- --------------------------------------------------------------------------------------- ---------- ---------------------------------------------------------------------------------------- ------- ---- ------
100 1 Which fire extinguisher is most suitable for a waste paper basket fire? Correct Powder 0 0 0
100 1 Which fire extinguisher is most suitable for a waste paper basket fire? Correct Carbon Dioxide (CO2) 1 0 0
100 1 Which fire extinguisher is most suitable for a waste paper basket fire? Correct Water (H2O) 2 1 1
100 1 Which fire extinguisher is most suitable for a waste paper basket fire? Correct Foam 3 0 0
100 2 What should your immediate action be on hearing a fire alarm? Correct Find all of your colleagues before making a speedy exit together 0 0 0
100 2 What should your immediate action be on hearing a fire alarm? Correct Collect all your valuables before making a speedy exit 1 0 0
100 2 What should your immediate action be on hearing a fire alarm? Correct Check the weather to see if you need your coat before leaving 2 0 0
100 2 What should your immediate action be on hearing a fire alarm? Correct Leave the building by the nearest exit, closing doors behind you if the rooms are empty 3 1 1
101 1 Which fire extinguisher is most suitable for a waste paper basket fire? Correct Powder 0 0 0
101 1 Which fire extinguisher is most suitable for a waste paper basket fire? Correct Carbon Dioxide (CO2) 1 0 0
101 1 Which fire extinguisher is most suitable for a waste paper basket fire? Correct Water (H2O) 2 1 1
101 1 Which fire extinguisher is most suitable for a waste paper basket fire? Correct Foam 3 0 0
101 2 Should you use lifts during a fire? Correct Yes 0 0 0
101 2 Should you use lifts during a fire? Correct No 1 1 1
101 3 Which part of a Carbon Dioxide (CO2) extinguisher should you not touch when operating? Incorrect The body of the extinguisher 0 0 1
101 3 Which part of a Carbon Dioxide (CO2) extinguisher should you not touch when operating? Incorrect The release trigger and the bottom of the extinguisher 1 0 0
101 3 Which part of a Carbon Dioxide (CO2) extinguisher should you not touch when operating? Incorrect The horn of the extinguisher 2 1 0
我明白这会造成大量的重复(因为每个答案都会重复提问),但没关系。
我有一个SQL Fiddle,我一直在从事样品数据设置。
这很有趣。您能否扩展以显示其他答案的答案详细程度,以便我可以看到如何在该级别进行审讯? – EvilDr
是的,我已经编辑了答案 – Serg