存储过程中的 TSQL 解析 XML

TSQL Parse XML in Stored Procedure(存储过程中的 TSQL 解析 XML)
本文介绍了存储过程中的 TSQL 解析 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有 4 个包含各种数据的表.

I have 4 tables which contain various pieces of data.

表 1 包含问题表 2 包含组表 3 包含选项表 4 包含带有个人资料数据列的职位申请

Table 1 contains Questions Table 2 contains Groups Table 3 contains Options Table 4 contains job applications with a column for Profile Data

生成配置文件数据并转换为 XML 并存储在表 4 的 profileData 列中.其格式如下:

The profile data is generated and converted to XML and stored in the profileData column on table 4. Its format is as so:

<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 号.

Now, I am creating a page where I need to re-create the job application with all of the settings those chose. When I create the XML, I am using the ID numbers that are tied to the actual question and information.

现在找出这些 ID 号的最佳方法是什么?我认为最好在存储过程中执行此操作,因为如果我在 javascript 中解析它时执行此操作,它将有很多数据库调用.

What is the best way to be able to find out what those ID numbers are now? I figured it would be best to do this in the stored procedure because If i did it while parsing it in javascript, it would have a lot of database calls.

只是不确定如何最好地解决这个问题或更改我的数据库结构.

Just not sure how to best go about this or change the structure of my database.

在示例中,我需要找出问题 ID = 2 等的问题所在.

In the example, I need to find out what the question is where question ID = 2 etc.

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

我认为您正在寻找 XQuery.

下面的示例解析您的 XML 并连接到一个问题表.它使用 nodes()、query() 和 value() 方法.

Example below parses out your XML and joins to a questions table. It uses the nodes(), query(), and value() methods.

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

这篇关于存储过程中的 TSQL 解析 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)