null をチェックしている場合は、次を使用できますCoalesce
。
select coalesce
(
(select 1 a where 1=2) --returns null
, (select 2 a where 2=3) --returns null
, (select 3 a where 4=4) --returns result
, 100 --default
) x
私にとって、これはケースステートメントよりもクリーンで読みやすく、同様に機能すると思います。
質問に含まれるコードと説明に基づいて、これは次のようになります。
CREATE FUNCTION [dbo].[SSS_GetInstructorApprovalDate](@lSSS_SectionsID INT)
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @dInstructApprDate DATETIME
, @dAddDropDate DATETIME
, @lSSS_OfferingsID INT
, @lSSS_TermsID INT
--, @lTemp INT = 0
--I suspect you don't want this bit; but uncomment if it's required (i.e. if you only want a value when there's a matching record in the secion table, but the record's approval date's null
--SELECT top 1 @lTemp = 1
--FROM SSS_SectionAcademicPeriods WITH (NOLOCK)
--WHERE SSS_SectionsID = @lSSS_SectionsID
--Fetch from section level, if present - Begin
--IF @lTemp = 1
--BEGIN
SELECT @dInstructApprDate = coalesce
(
(
SELECT InstructorApprovalDate
FROM SSS_SectionAcademicPeriods with(nolock)
where SSS_SectionsID = @lSSS_SectionsID
)
,
(
select InstructorApprovalDate
from SSS_OfferingAcademicPeriods
where SSS_OfferingsID = @lSSS_OfferingsID
)
,
(
select InstructorApprovalDate
from SSS_TermsAcademicPeriods
where SSS_OfferingsID = @lSSS_TermsID
)
,
(
SELECT AbsoluteExpireDate
FROM SSS_SectionAcademicPeriods with(nolock)
where SSS_SectionsID = @lSSS_SectionsID
)
,
(
select AbsoluteExpireDate
from SSS_OfferingAcademicPeriods
where SSS_OfferingsID = @lSSS_OfferingsID
)
,
(
select AbsoluteExpireDate
from SSS_TermsAcademicPeriods
where SSS_OfferingsID = @lSSS_TermsID
)
,
(
SELECT WaitListDate
FROM SSS_SectionAcademicPeriods with(nolock)
where SSS_SectionsID = @lSSS_SectionsID
)
,
(
select WaitListDate
from SSS_OfferingAcademicPeriods
where SSS_OfferingsID = @lSSS_OfferingsID
)
,
(
select WaitListDate
from SSS_TermsAcademicPeriods
where SSS_OfferingsID = @lSSS_TermsID
)
)
--END
return cast(@dInstructApprDate as varchar(20)) --probably
END
注意: 各クエリにかかる時間によっては、少し異なるアプローチが必要になる場合があります。これが代替案です/それがどのように適合するか教えてください:
CREATE FUNCTION [dbo].[SSS_GetInstructorApprovalDate](@lSSS_SectionsID INT)
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @dInstructApprDate DATETIME
, @dInstructApprDate2 DATETIME
, @dInstructApprDate3 DATETIME
, @dAddDropDate DATETIME
, @lSSS_OfferingsID INT
, @lSSS_TermsID INT
--, @lTemp INT = 0
--I suspect you don't want this bit; but uncomment if it's required (i.e. if you only want a value when there's a matching record in the secion table, but the record's approval date's null
--SELECT top 1 @lTemp = 1
--FROM SSS_SectionAcademicPeriods WITH (NOLOCK)
--WHERE SSS_SectionsID = @lSSS_SectionsID
--Fetch from section level, if present - Begin
--IF @lTemp = 1
--BEGIN
SELECT @dInstructApprDate = InstructorApprovalDate
, @dInstructApprDate2 = AbsoluteExpireDate
, @dInstructApprDate3 = WaitListDate
FROM SSS_SectionAcademicPeriods with(nolock)
where SSS_SectionsID = @lSSS_SectionsID
if @dInstructApprDate is null
select @dInstructApprDate = InstructorApprovalDate
, @dInstructApprDate2 = isnull(@dInstructApprDate2, AbsoluteExpireDate)
, @dInstructApprDate3 = isnull(@dInstructApprDate3, WaitListDate)
from SSS_OfferingAcademicPeriods
where SSS_OfferingsID = @lSSS_OfferingsID
if @dInstructApprDate is null
select @dInstructApprDate = InstructorApprovalDate
, @dInstructApprDate2 = isnull(@dInstructApprDate2, AbsoluteExpireDate)
, @dInstructApprDate3 = isnull(@dInstructApprDate3, WaitListDate
from SSS_TermsAcademicPeriods
where SSS_OfferingsID = @lSSS_TermsID
set @dInstructApprDate = coalesce(@dInstructApprDate, @dInstructApprDate2, @dInstructApprDate3)
--END
return cast(@dInstructApprDate as varchar(20)) --probably
END