SQL Server 多条记录的某个字段拼接

USE [FM_Dev]
GO
/****** 对象:  UserDefinedFunction [dbo].[GetClassNameByStudentCode]    脚本日期: 05/23/2014 17:20:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author: 
-- Create date: 2014年5月7日
-- Description: 根据学员编号获取学员报读的班级名称拼接字符串
-- =============================================
CREATE FUNCTION [dbo].[GetClassNameByStudentCode] 
(
    @StudentCode varchar(50)
)
RETURNS varchar(100)
AS
BEGIN

    declare @ClassName nvarchar(200)
    set @ClassName=''

    select @ClassName = @ClassName + ClassName + ',' 
from FM_Class cls join FM_StudentClass sc on sc.ClassCode=cls.ClassCode
where sc.code=@StudentCode order by cls.Id if @ClassName<>'' begin select @ClassName = left(@ClassName, len(@ClassName)-1) end return @ClassName END

 

posted @ 2014-05-23 22:49  0611163  阅读(1245)  评论(0编辑  收藏  举报