如何在 sql server 2008 中为多对多关系生成嵌套 XML?
情况是有课程和讲师,并且它们具有多对多关系(1 个课程可以有多个讲师,1 个讲师可以教授多个课程)
以下 XML 重复了课程名称:
SELECT
C.COURSE_ID "@course_id"
, C.COURSE_NAME "Course_Name"
--get lecturer id(s) of the course
, CL.LECTURER_ID
FROM
COURSE C LEFT JOIN COURSE_LECTURER CL ON C.COURSE_ID = CL.COURSE_ID
--LEFT JOIN AS ALL COURSES DON'T HAVE LECTURERS COULD BE INNER :)
FOR XML PATH('Course'), ROOT('Program')
它给了我以下输出(片段):
<Course course_id="ISFND 1.1">
<Course_Name>Relational Databases and SQL</Course_Name>
<LECTURER_ID>ME123</LECTURER_ID>
</Course>
<Course course_id="ISFND 1.1">
<Course_Name>Relational Databases and SQL</Course_Name>
<LECTURER_ID>HS123</LECTURER_ID>
</Course>
但是我想要的输出是:
<Course course_id="ISFND 1.1">
<Course_Name>Relational Databases and SQL</Course_Name>
<LECTURER_ID>ME123</LECTURER_ID>
<LECTURER_ID>HS123</LECTURER_ID>
</Course>
我想我的sql中存在一些问题,对于多对多来说,可能我需要对每个使用IN或类似的东西。任何帮助将不胜感激。谢谢。
The situation is there are courses and lecturers and they have a many to many relationship (1 course can have many lecturers, 1 lecturer can teach many courses)
The following XML repeats the course name:
SELECT
C.COURSE_ID "@course_id"
, C.COURSE_NAME "Course_Name"
--get lecturer id(s) of the course
, CL.LECTURER_ID
FROM
COURSE C LEFT JOIN COURSE_LECTURER CL ON C.COURSE_ID = CL.COURSE_ID
--LEFT JOIN AS ALL COURSES DON'T HAVE LECTURERS COULD BE INNER :)
FOR XML PATH('Course'), ROOT('Program')
It gives me the following output (snippet):
<Course course_id="ISFND 1.1">
<Course_Name>Relational Databases and SQL</Course_Name>
<LECTURER_ID>ME123</LECTURER_ID>
</Course>
<Course course_id="ISFND 1.1">
<Course_Name>Relational Databases and SQL</Course_Name>
<LECTURER_ID>HS123</LECTURER_ID>
</Course>
But the output I want is:
<Course course_id="ISFND 1.1">
<Course_Name>Relational Databases and SQL</Course_Name>
<LECTURER_ID>ME123</LECTURER_ID>
<LECTURER_ID>HS123</LECTURER_ID>
</Course>
I guess there is some problem in my sql for many to many may be I need to use IN or something like for each. Any help will be appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)