SQL Server 2005 - TSQL For Xml 查询以获取分组的部门和员工
对于包含 3 个表的数据库:
- Company
- Department
- EmployeeInDepartment
我想要一个如下所示的 xml:
<Companies>
<Company>
<CompanyName></CompanyName>
<CompanyId></CompanyId>
<..></..>
<Departments>
<Name></Name>
<..></..>
<Employees>
<Employee>
<FirstName></FirstName>
<LastName></LastName>
.. .. ..
</Employee>
.. .. ..
</Employees>
</Departments>
.. .. ..
</Company>
.. .. ..
</Companies>
公司重复(标签),公司内部部门重复(标签),部门内部员工重复(标签),我的意思是那里这些元素的数量不只一个。不是数据。
关系
公司
和部门
通过中的
表。FK
关联链接到Company
表中的CompanyId
的 DepartmentDepartment
和EmployeeInDepartment
通过EmployeeInDepartment
表中的“FK”关联,该表链接到DepartmentId
中>部门表。
查询:
在 PUBS 数据库上执行以下命令并检查 xml,将会有多个 id 为 10 的 Jobs
select jobs.job_id 'JobId',
job_desc 'Desc',
(
select emp_id 'EmployeeId',fname 'FirstName',lname 'LastName' from employee where job_id = jobs.job_id for xml path('Emploees'),type
)
from jobs
inner join
employee on jobs.job_id = employee.job_id
for xml path('employees')
For a database holding 3 tables:
- Company
- Department
- EmployeeInDepartment
I would like to a xml like below:
<Companies>
<Company>
<CompanyName></CompanyName>
<CompanyId></CompanyId>
<..></..>
<Departments>
<Name></Name>
<..></..>
<Employees>
<Employee>
<FirstName></FirstName>
<LastName></LastName>
.. .. ..
</Employee>
.. .. ..
</Employees>
</Departments>
.. .. ..
</Company>
.. .. ..
</Companies>
Where the company is repeating (tags), departments inside company are repeating (tags) and Employee inside departments are repeating (tags) by repeating I mean there are more than one number of these element & not the data.
Relations
Company
andDepartment
are related throughFK
inDepartment
table that links toCompanyId
inCompany
table.Department
andEmployeeInDepartment
are related through 'FK' inEmployeeInDepartment
table that links toDepartmentId
inDepartment
table.
Query:
execute the below on PUBS database and check the xml, it will have more than one Jobs with id 10
select jobs.job_id 'JobId',
job_desc 'Desc',
(
select emp_id 'EmployeeId',fname 'FirstName',lname 'LastName' from employee where job_id = jobs.job_id for xml path('Emploees'),type
)
from jobs
inner join
employee on jobs.job_id = employee.job_id
for xml path('employees')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要执行“嵌套”
FOR XML
语句 - 这应该可以为您提供所需的内容。请参阅 Richard Dingwall 的 使用 SQL Server 嵌套 FOR XML 结果,展示了如何执行此操作。当然,您可以轻松嵌套两层以上......
You need to do "nested"
FOR XML
statements - that should give you what you're looking for.See e.g. Richard Dingwall's Nested FOR XML results with SQL Server that shows how to do this. Of course, you can easily nest more than two levels...
不完全确定你想要这个,但这应该给你一个开始。
结果:
您不应该在每个部分连接所有表。主查询仅查询根级别所需的信息,子查询仅使用 where 子句
where jobs.job_id = emp_id
来查询根级别所需的信息,该子句会过滤 root 级别中的行。子查询,以便您只能在子节点中获取所需的行。Not entirely sure how you want this but this should give you a start.
Result:
You should not join all your tables in every part. The main query only queries the information that is needed for the root level and the sub-queries only query the information needed there with a where clause
where jobs.job_id = employee.emp_id
that filters the rows in the sub queries so you only get the rows you want in the child nodes.