郁闷 自定义函数这句代码怎么无效呢
CREATE FUNCTION [dbo].[Fn_ShowDepartment](@ID int)RETURNS varchar(500)ASBEGINDeclare @Count intDeclare @Index intDeclare @Department varchar(200)Declare @Mark varchar(500)set @Count = (select count(*) from OA_GongGao_Detail where GGID = @ID)set @Index=0set @Department=''set @Mark=''while @Index < @Countbeginset @Index = @Index+1select @Department =(select top 1 DepartmentName from(select top(@Index) GG.ID as ID,DT.DepartmentName as DepartmentName,GG.ToDepartmentId as ToDepartmentId,GGID as GGID from OA_GongGao_Detail GG left join Sys_Department DT on GG.ToDepartmentId = DT.id where GG.GGID=@ID order by GG.ID) a order by ID desc)[color=#FF0000]set @Mark = @Mark+@Department+','[/color]endif len(@Mark)>0beginset @Mark = left(@Mark,len(@Mark)-1)endreturn @MarkEND
Declare @Department varchar(200)Declare @Mark varchar(500)set @Mark=''set @Department=''select @Department='设计部'set @Mark = @Mark+@Department+','print @Mark
[解决办法]
這樣也可以,
set @Mark = @Mark + case when @Department is null then '' else @Department+',' end