我有4个表,其中包含各种数据。
表1包含问题 表2包含组 表3包含选项 表4包含具有“配置文件数据”列的作业应用程序
生成配置文件数据并将其转换为XML并存储在表4的profileData列中。其格式如下:
<proficiencies>
<question>
<questionID>2</questionID>
<questionGroup>2</questionGroup>
<questionOptions>
<option>19</option>
<option>20</option>
<option>31</option>
</questionOptions>
</question>
<question>
<questionID>1</questionID>
<questionGroup>1</questionGroup>
<questionOptions>
<option>20</option>
<option>29</option>
<option>21</option>
</questionOptions>
</question>
<question>
<questionID>3</questionID>
<questionGroup>2</questionGroup>
<questionOptions>
<option>18</option>
<option>29</option>
</questionOptions>
</question>
</proficiencies>
现在,我正在创建一个页面,我需要使用所选的所有设置重新创建作业应用程序。当我创建XML时,我使用的是与实际问题和信息相关的ID号。
能够找出现在这些身份证号码的最佳方式是什么?我认为最好在存储过程中执行此操作,因为如果我在javascript中解析它时,它会有很多数据库调用。
不确定如何最好地解决这个问题或更改数据库的结构。
在这个例子中,我需要找出问题ID = 2等问题。
有更好的方法吗?
答案 0 :(得分:0)
我认为您正在寻找XQuery。
下面的示例解析您的XML并加入问题表。它使用nodes(),query()和value()方法。
with MyXML
as
(
select
1 as MyXMLDataID
,cast('
<proficiencies>
<question>
<questionID>2</questionID>
<questionGroup>2</questionGroup>
<questionOptions>
<option>19</option>
<option>20</option>
<option>31</option>
</questionOptions>
</question>
<question>
<questionID>1</questionID>
<questionGroup>1</questionGroup>
<questionOptions>
<option>20</option>
<option>29</option>
<option>21</option>
</questionOptions>
</question>
<question>
<questionID>3</questionID>
<questionGroup>2</questionGroup>
<questionOptions>
<option>18</option>
<option>29</option>
</questionOptions>
</question>
</proficiencies>' as xml) as MyXMLData),
MyQuestionGroups
as
(
select 1 as questionGroup, 'Education' as questionGroup_description
union
select 2 as questionGroup, 'Experience' as questionGroup_description
),
MyQuestions
as
(
select 1 as questionGroup, 1 as questionID, 'High school attendance' as question
union
select 2 as questionGroup, 2 as questionID, 'Alchemy experience' as question
union
select 2 as questionGroup, 3 as questionID, 'Arbitrage experience' as question
),
MyOptions
as
(
select 18 as optionID, '1 year' as option_description
union
select 19 as optionID, '2 year' as option_description
union
select 20 as optionID, '3 years' as option_description
union
select 21 as optionID, '4 year' as option_description
union
select 29 as optionID, '5 year' as option_description
union
select 31 as optionID, '6 years' as option_description
)
SELECT MyXML.MyXMLDataID
,t1.questionID.query('.') as questionID_node
,q.question
FROM MyXML
CROSS APPLY MyXMLData.nodes('/proficiencies/question/questionID') as t1(questionID)
INNER JOIN MyQuestions q
on q.questionID = t1.questionID.value('.', 'int')
返回此结果:
-----------------------------------------------
MyXMLDataID questionID_node question
-----------------------------------------------
1 <questionID>2</questionID> Alchemy experience
1 <questionID>1</questionID> High school attendance
1 <questionID>3</questionID> Arbitrage experience