功能导致错误

时间:2015-12-15 10:45:36

标签: sql sql-server-2008-r2

我想为某个目的创建一个函数,函数就是这样,但它不起作用。

CREATE FUNCTION FNLVDAYS 
  (@MONTH SMALLINT,@YEAR SMALLINT)
  RETURNS INT
 AS
  BEGIN
  DECLARE @LVDAYS INT

  SELECT COALESCE((SELECT SUM (LVDAYS) FROM LeaveTran  WHERE
       LeaveTran.EMPCODE=LeaveMaster.EMPCODE AND LeaveTran.LVTYPE=LeaveMaster.LVTYPE   AND MONTH(LV_FROM)=@MONTH AND YEAR(LV_FROM)=@YEAR ),0) AVAILED
FROM LEAVEMASTER 

 RETURN @LVDAYS
 END

提供此错误:

     Msg 444, Level 16, State 2, Procedure FNLVDAYS, Line 8
     Select statements included within a function cannot return data to a client.

1 个答案:

答案 0 :(得分:2)

希望这会有所帮助......

SELECT @LVDAYS=COALESCE((SELECT SUM (LVDAYS) FROM LeaveTran  WHERE
       LeaveTran.EMPCODE=LeaveMaster.EMPCODE AND LeaveTran.LVTYPE=LeaveMaster.LVTYPE   AND MONTH(LV_FROM)=@MONTH AND YEAR(LV_FROM)=@YEAR ),0) AVAILED
FROM LEAVEMASTER