SQL 多行合并成一行

1 SELECT UserPhone FROM dbo.sys_user_manager
2 WHERE UserCardID IN (
3 SELECT gu2.UserID FROM GroupUser AS gu 
4 INNER JOIN GroupUser AS gu2 ON gu.BelongedTo = gu2.BelongedTo AND gu.OffName = gu2.OffName AND gu.GroupName = gu2.GroupName
5 WHERE gu.MovePhone ='18551691779')
View Code
处理后的:

DECLARE @RetVal varchar(8000)
SET @RetVal = ''
SELECT @RetVal= UserPhone+','+@RetVal FROM dbo.sys_user_manager
WHERE UserCardID IN (
SELECT gu2.UserID FROM GroupUser AS gu
INNER JOIN GroupUser AS gu2 ON gu.BelongedTo = gu2.BelongedTo AND gu.OffName = gu2.OffName AND gu.GroupName = gu2.GroupName
WHERE gu.MovePhone =@cid)
IF LEN(@RetVal) > 0
BEGIN
SET @RetVal = LEFT(@RetVal, LEN(@RetVal) - 1)
END

RETURN @RetVal

 

posted @ 2016-01-15 16:07  管学文  阅读(210)  评论(0编辑  收藏  举报