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。
答案 0 :(得分: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)