-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。

4

1 回答 1

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)
于 2013-10-28T10:20:17.937 回答