Concatenating Row Values

↘锁芯ラ 提交于 2019-12-02 17:21:22

问题


I was using Microsoft SQL server 2005 and was able to concatenate row values based on the following query:

SELECT e1.EMP_ID,
( SELECT cast(Sector_ID as varchar(10)) + ';'
FROM Employee_Sector_relationship e2
WHERE e2.Emp_ID = e1.Emp_ID
ORDER BY Sector_ID
FOR XML PATH('') ) AS Sectors
FROM Employee_Sector_Relationship e1
GROUP BY Emp_ID

But it doesn't work in Microsoft Server 2000. It gives me an error near the for keyword. Can anyone help me to concatenate the row values in Microsoft Server 2000?


回答1:


This is a technique that should work for you. You can execute this in one batch statement if you wish:

DECLARE @EmployeeList varchar(100)

SELECT @EmployeeList = COALESCE(@EmployeeList + ', ', '') + 
   CAST(Emp_UniqueID AS varchar(5))
FROM SalesCallsEmployees
WHERE SalCal_UniqueID = 1

SELECT @EmployeeList

For more information, see Using COALESCE to Build Comma-Delimited String.




回答2:


FOR XML PATH

is not available in sql server 2000.

This article discusses different approached for concatenating row values: Concatenating Row Values in Transact-SQL



来源:https://stackoverflow.com/questions/2064940/concatenating-row-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!