2013-10-28 268 views
-2
ALTER FUNCTION [dbo].[getCourseCost] 
(
    @CourseCode varchar(50), 
    @Year nchar(10), 
    @Period nchar(10) 
) 
RETURNS int 
AS 
BEGIN 
DECLARE @Pnr_personal varchar(50) 
DECLARE @Timlön_personal int 
DECLARE @Antal_timmar_personal int 
DECLARE @Kostnad_personal int 
DECLARE @Kostnad_labass int 

SELECT @Pnr_personal = Personnummer FROM Bemannas_Av WHERE Period = @Period AND Läsår =     @Year AND Kurskod = @CourseCode 
SELECT @Timlön_personal = Timlön FROM Personal WHERE Personnummer = @Pnr_personal 
SELECT @Antal_timmar_personal = Antal_Timmar FROM Bemannas_Av WHERE Period = @Period AND Läsår = @Year AND Kurskod = @CourseCode 
SELECT @Kostnad_labass = (Antal_timmar * Timlön) FROM Labass WHERE Period = @Period AND Läsår = @Year AND Kurser = @CourseCode 

SET @Kostnad_personal = @Timlön_personal * @Antal_timmar_personal 
IF @Kostnad_personal = NULL 
BEGIN 
SET @Kostnad_personal = 0 
END 

IF @Kostnad_labass = NULL 
BEGIN 
SET @Kostnad_labass = 0 
END 

RETURN @Kostnad_personal + @Kostnad_labass 
END 

即使沒有使用的值在數據庫中爲NULL,它仍會返回NULL。函數返回null

+0

難道你選擇的一個返回多行嗎? – Ralf

回答

1

該零件有問題@Kostnad_personal=null意味着什麼。

IF @Kostnad_personal is NULL 
BEGIN 
SET @Kostnad_personal = 0 
END 

IF @Kostnad_labass is NULL 
BEGIN 
SET @Kostnad_labass = 0 
END 

或者使用:

RETURN ISNULL(@Kostnad_personal,0) + ISNULL(@Kostnad_labass,0) 
+0

感謝的人,它的工作 – Jacco

+0

歡迎您高興地被接受。 –

+0

請勾選以接受。 –