I am trying to read data from XML and display the results in tabular for in SQL Server.
The XML data:
<GradeSets>
<GradeSet name="ALevel" descriptor="ALevelGrade" equivalence="1" allowedVariants="SplitGrades,FineGrades" va_multiplier="100">
<Grade id="A*" points="140" success="true" />
<Grade id="A" points="120" success="true" />
<Grade id="B" points="100" success="true" />
<Grade id="C" points="80" success="true" />
<Grade id="D" points="60" success="true" />
<Grade id="E" points="40" success="true" />
<Grade id="U" points="0" success="false" />
<Grade id="X" points="0" success="false" />
<Grade id="Q" points="0" success="false">
<Meta key="excludeStudent">true</Meta>
</Grade>
<GradeGroup id="A*>E">
<Grade>A*</Grade>
<Grade>A</Grade>
<Grade>B</Grade>
<Grade>C</Grade>
<Grade>D</Grade>
<Grade>E</Grade>
</GradeGroup>
<GradeGroup id="A*>C">
<Grade>A*</Grade>
<Grade>A</Grade>
<Grade>B</Grade>
<Grade>C</Grade>
</GradeGroup>
<GradeGroup id="A*>B">
<Grade>A*</Grade>
<Grade>A</Grade>
<Grade>B</Grade>
</GradeGroup>
</GradeSet>
<GradeSet name="ALevel-Double" descriptor="ALevelGrade" equivalence="2" va_multiplier="200">
<Grade id="A*A*" points="280" success="true" />
<Grade id="A*A" points="260" success="true" />
<Grade id="AA" points="240" success="true" />
<Grade id="AB" points="220" success="true" />
<Grade id="BB" points="200" success="true" />
<Grade id="BC" points="180" success="true" />
<Grade id="CC" points="160" success="true" />
<Grade id="CD" points="140" success="true" />
<Grade id="DD" points="120" success="true" />
<Grade id="DE" points="100" success="true" />
<Grade id="EE" points="80" success="true" />
<Grade id="U" points="0" success="false" />
<Grade id="X" points="0" success="false" />
<Grade id="Q" points="0" success="false">
<Meta key="excludeStudent">true</Meta>
</Grade>
<GradeGroup id="A*A*>EE">
<Grade>A*A*</Grade>
<Grade>A*A</Grade>
<Grade>AA</Grade>
<Grade>AB</Grade>
<Grade>BB</Grade>
<Grade>BC</Grade>
<Grade>CC</Grade>
<Grade>CD</Grade>
<Grade>DD</Grade>
<Grade>DE</Grade>
<Grade>EE</Grade>
</GradeGroup>
<GradeGroup id="A*A*>CC">
<Grade>A*A*</Grade>
<Grade>A*A</Grade>
<Grade>AA</Grade>
<Grade>AB</Grade>
<Grade>BB</Grade>
<Grade>BC</Grade>
<Grade>CC</Grade>
</GradeGroup>
<GradeGroup id="A*A*>BB">
<Grade>A*A*</Grade>
<Grade>A*A</Grade>
<Grade>AA</Grade>
<Grade>AB</Grade>
<Grade>BB</Grade>
</GradeGroup>
</GradeSet>
</GradeSets>
I wrote this query, but I cannot get it to work:
SELECT
MY_XML.GradeSet.query('GradeSet').value('.', 'VARCHAR(20)') ,
MY_XML.GradeSet.query('GradeGroup').value('.', 'VARCHAR(20)')
FROM (SELECT CAST(MY_XML AS xml)
FROM OPENROWSET(BULK 'C:\Users\johno\OneDrive\Desktop\MyData\MyDataImport.xml', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
CROSS APPLY MY_XML.nodes('GradeSets/GradeSet') AS MY_XML (GradeSet);
Current Output
(No column name) (No column name)
A*ABCDEA*ABCA*AB
A*A*A*AAAABBBBCCCCDD
Expected Output guild

GradeSetrather thanGradeto match the name:MY_XML.GradeSet.query(.... Also, note that the SQL Server service account will need read permissions to the folder/file.Gradehad no text properly in your question either, and there are multiple elements and you don't tell SQL Server which one you want. What are your expected results here?Gradecolumn name toGradeSetin the XML method as I suggested. This will fix the error but the results will concatenate all the text nodes underGradeGroup, which is probably not what you want.