首页 > 代码库 > 使用for xml path()进行字符串拼接

使用for xml path()进行字符串拼接

Department表

 技术分享

 

Employee表

 技术分享

 

查询结果:

 技术分享

 

查询每个部门的人员名称

dbo.View_1:

SELECT   dbo.Department.DepName, dbo.Employee.EmpName

FROM      dbo.Department INNER JOIN

                dbo.Employee ON dbo.Department.DepID = dbo.Employee.DepID

查询:

select DepName,STUFF((select ‘,‘+ EmpName  from dbo.View_1  where dbo.View_1.DepName=b.DepName for xml path(‘‘)),1,1,‘‘)as name

from dbo.View_1 as b group by DepName

 

使用for xml path()进行字符串拼接