Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
BruceT
Yak Posting Veteran
78 Posts |
Posted - 2011-07-08 : 11:40:40
|
| Hi all,I'm not up to speed on the XML options in SQL Server so any help would be greatly appreciated. Given the following tables and example XML result is it possible to create a query to generate the XML result?Tablesdbo.assessments id guid assessmentdate datetime casenumber char(6)dbo.questions id guid assessmentid guid assessmenttype int questionnumber int answer intBasic selectselect * from dbo.assessments a join dbo.questions q on a.id = q.assessmentidXML result desired<Assessments> <Assessment AssessmentDate="2010-09-23T00:00:00" CaseNumber="000001" AssessmentType="1"> <Questions> <QuestionNumber="1" Answer="-1"/> <QuestionNumber="2" Answer="1"/> <QuestionNumber="3" Answer="1"/> <QuestionNumber="4" Answer="0"/> <QuestionNumber="5" Answer="1"/> </Questions> </Assessment> <Assessment AssessmentDate="2010-09-24T00:00:00" CaseNumber="000001" AssessmentType="3"> <Questions> <QuestionNumber="2" Answer="1"/> <QuestionNumber="3" Answer="1"/> </Questions> </Assessment> <Assessment AssessmentDate="2010-09-15T00:00:00" CaseNumber="000002" AssessmentType="4"> <Questions> <QuestionNumber="1" Answer="1"/> <QuestionNumber="2" Answer="1"/> <QuestionNumber="3" Answer="1"/> </Questions> </Assessment>etc.etc.etc.</Assessments> |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-07-08 : 13:12:30
|
The XML you posted is not well-formed, so SQL XML won't be able to generate it. You can see what I mean if you assign your desired output to an xml variable and try to do a select like this:DECLARE @x XML;SET @x = -- I copied the XML fragment from your posting'<Assessments><Assessment AssessmentDate="2010-09-23T00:00:00" CaseNumber="000001" AssessmentType="1"><Questions><QuestionNumber="1" Answer="-1"/><QuestionNumber="2" Answer="1"/><QuestionNumber="3" Answer="1"/><QuestionNumber="4" Answer="0"/><QuestionNumber="5" Answer="1"/></Questions></Assessment><Assessment AssessmentDate="2010-09-24T00:00:00" CaseNumber="000001" AssessmentType="3"><Questions><QuestionNumber="2" Answer="1"/><QuestionNumber="3" Answer="1"/></Questions></Assessment><Assessment AssessmentDate="2010-09-15T00:00:00" CaseNumber="000002" AssessmentType="4"><Questions><QuestionNumber="1" Answer="1"/><QuestionNumber="2" Answer="1"/><QuestionNumber="3" Answer="1"/></Questions></Assessments>';SELECT @x; I tried to guess what you might want, but not very good at that, so I miserably failed. If you can post the well-formed XML that you want to generate I am sure someone on the forum would be able to offer suggestions. Some sample data would make that a lot easier on them as well. |
 |
|
|
BruceT
Yak Posting Veteran
78 Posts |
Posted - 2011-07-08 : 13:35:06
|
Sorry typo on my part, there should be a space between Question and number ie.<Question Number="1" Answer="-1"/>quote: Originally posted by sunitabeck The XML you posted is not well-formed, so SQL XML won't be able to generate it. You can see what I mean if you assign your desired output to an xml variable and try to do a select like this:DECLARE @x XML;SET @x = -- I copied the XML fragment from your posting'<Assessments><Assessment AssessmentDate="2010-09-23T00:00:00" CaseNumber="000001" AssessmentType="1"><Questions><QuestionNumber="1" Answer="-1"/><QuestionNumber="2" Answer="1"/><QuestionNumber="3" Answer="1"/><QuestionNumber="4" Answer="0"/><QuestionNumber="5" Answer="1"/></Questions></Assessment><Assessment AssessmentDate="2010-09-24T00:00:00" CaseNumber="000001" AssessmentType="3"><Questions><QuestionNumber="2" Answer="1"/><QuestionNumber="3" Answer="1"/></Questions></Assessment><Assessment AssessmentDate="2010-09-15T00:00:00" CaseNumber="000002" AssessmentType="4"><Questions><QuestionNumber="1" Answer="1"/><QuestionNumber="2" Answer="1"/><QuestionNumber="3" Answer="1"/></Questions></Assessments>';SELECT @x; I tried to guess what you might want, but not very good at that, so I miserably failed. If you can post the well-formed XML that you want to generate I am sure someone on the forum would be able to offer suggestions. Some sample data would make that a lot easier on them as well.
|
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-07-08 : 14:42:31
|
The query below parses, but I am not sure about couple of things:1. What is the column that relates the two tables? See comment in code. If that is not the correct join condition, change appropriately.2. The code parses, but the nesting hierarchy may not be exactly correct. If it does not do the right things, can you post some sample data? SELECT a.AssessmentDate AS [Assessment/@AssessmentDate], a.casenumber AS [Assessment/@CaseNumber], ( SELECT questionnumber AS [Question/@Number], answer AS [Question/@Answer] FROM questions q WHERE q.assessmentid = a.id -- Is this the correct join? FOR XML PATH(''),TYPE ) AS [Assessment/Questions]FROM Assessments aFOR XML PATH(''), ROOT('Assessments'); |
 |
|
|
BruceT
Yak Posting Veteran
78 Posts |
Posted - 2011-07-08 : 14:49:44
|
quote: Originally posted by sunitabeck The query below parses, but I am not sure about couple of things:1. What is the column that relates the two tables? See comment in code. If that is not the correct join condition, change appropriately.2. The code parses, but the nesting hierarchy may not be exactly correct. If it does not do the right things, can you post some sample data? SELECT a.AssessmentDate AS [Assessment/@AssessmentDate], a.casenumber AS [Assessment/@CaseNumber], ( SELECT questionnumber AS [Question/@Number], answer AS [Question/@Answer] FROM questions q WHERE q.assessmentid = a.id -- Is this the correct join? FOR XML PATH(''),TYPE ) AS [Assessment/Questions]FROM Assessments aFOR XML PATH(''), ROOT('Assessments');
Yep that's it. Thanks a bunch, sunitabeck! |
 |
|
|
|
|
|
|
|