鉴于您正在检查 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