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。