2

次のコード(一部) を含むスカラー値関数を作成しました。

declare @start_time22 time(7);
select @start_time22=Courses.[Course start time] from Courses where [Course ID]=@val1 and [Course days]='monday wednesday';

if(@start_time22 is not null)
    begin
        IF (@start_time not between @start_time22 and @end_time22)
            BEGIN
                SET @Result = 1
            END
        ELSE
            BEGIN
                SET @Result = 0
            END
        IF (@end_time not between @start_time22 and @end_time22)
            BEGIN
                SET @Result = 1
            END
        ELSE
            BEGIN
                SET @Result = 0
            END
    end
else 
    begin
        set @Result = 5
    end


RETURN @Result

関数は常に値 " 5 " を返すので、そもそも型時間を比較できるかどうか疑問に思っていました... それとも私のコードに何か問題がありますか?

4

1 に答える 1

1

おそらく、クエリを次のようにする必要があります。

select @start_time22=Courses.[Course start time]
from Courses
where [Course ID]=@val1 and [Course days] in ('monday', 'wednesday');

コメントが基本的に説明しているように、変数を宣言すると、値は NULL に設定されます。別の値に設定する場合は、次のような構文を使用できます。

declare @str varchar(255) = 'My favorite string'

クエリは結果を返さないため、変数に値が割り当てられることはありません。NULL 値のままです。

また、変数を使用してからロジックを実行するのではなく、caseロジックを使用して、クエリ自体で作業を行うことができます。selectif

select @result = (case when <whatever . . . )
from Courses
where [Course ID]=@val1 and [Course days] in ('monday', 'wednesday');
于 2012-12-19T14:14:01.860 に答える