I have 4 tables which contain various pieces of data.
Table 1 contains Questions
Table 2 contains Groups
Table 3 contains Options
Table 4 contains job applications with a column for Profile Data
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>
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.
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.
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?
I think you are looking for XQuery.
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')
It returns this result:
-----------------------------------------------
MyXMLDataID questionID_node question
-----------------------------------------------
1 <questionID>2</questionID> Alchemy experience
1 <questionID>1</questionID> High school attendance
1 <questionID>3</questionID> Arbitrage experience