2

我必须从一个名为 section 的表中获得教师批准日期。如果该表没有日期(空),那么我必须从提供表中获取日期,即使该表没有我要查找的内容,也要从 Term 表中获取。

并且如果所有三个都为空,那么我需要以与讲师批准日期相同的方式获取 absoluteExpireDate。

并且如果 absoluteExpireDate 在所有三个表中也是空的,那么我需要以类似的方式获取 WaitList 日期。

我如何创建一个案例语句来处理它?

到目前为止,这就是我所拥有的:

SELECT  @dInstructApprDate = case when a.InstructorApprovalDate is null  
        then
             select @dInstructApprDate = instructorapprovaldate 
             from SSS_OfferingAcademicPeriods   
             where SSS_OfferingRegPeriods.SSS_OfferingsID = @lSSS_OfferingsID

我不确定为什么它不喜欢我在“THEN”中使用 select 语句

任何帮助,将不胜感激。

这是我到目前为止获得的功能:

 CREATE FUNCTION [dbo].[SSS_GetInstructorApprovalDate](@lSSS_SectionsID INT)      
 RETURNS VARCHAR(20)    
 AS      

          BEGIN      

  DECLARE   
 @dInstructApprDate DATETIME,  
 @dAddDropDate  DATETIME,  
 @lTemp    INT,  
 @lSSS_OfferingsID INT,  
 @lSSS_TermsID  INT  

 SET @lTemp = 0  

 SELECT   
 @lTemp = 1  
 WHERE   
 EXISTS (SELECT 1 FROM SSS_SectionAcademicPeriods WITH (NOLOCK) WHERE SSS_SectionsID = @lSSS_SectionsID)  

--Fetch from section level, if present - Begin  
 IF @lTemp = 1  
BEGIN  

  SELECT  @dInstructApprDate = case when a.InstructorApprovalDate is null  
  then   
   (select instructorapprovaldate from SSS_OfferingAcademicPeriods where SSS_OfferingRegPeriods.SSS_OfferingsID = @lSSS_OfferingsID)

  else   
 InstructorApprovalDate  
end   
  FROM  
   SSS_SectionAcademicPeriods a WITH (NOLOCK)  
  where   
   SSS_SectionsID = @lSSS_SectionsID
4

5 回答 5

2

鉴于您正在检查 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
于 2013-09-11T18:36:20.643 回答
1

在这种情况下,您应该使用IF 语句。

CASE 用于选择标量值,(也可以通过查询检索)。可以使用 IF 以更易读的方式为相关案例选择相关查询。

您的代码应如下所示(伪):

IF(Some condition)
BEGIN
     SELECT ...
     FROM...   

END
ELSE IF (Some condition)
BEGIN
    SELECT ...
    FROM...   
END
于 2013-09-11T18:27:52.217 回答
1

如果没有整个查询,就很难说,a代表什么?看起来您case是更大查询的一部分,但是

SELECT
    @dInstructApprDate = 
         case
             when a.InstructorApprovalDate is null  
             then 
             (
                 select o.InstructorApprovalDate 
                 from SSS_OfferingAcademicPeriods as o
                 where o.SSS_OfferingsID = @lSSS_OfferingsID
             )
-- ...
-- you have from clause here?
-- ...

我认为您的查询可以大大简化,但在我看到整个查询之前不能说

更新

select @dInstructApprDate = InstructorApprovalDate
from SSS_SectionAcademicPeriods
where SSS_SectionsID = @lSSS_SectionsID

if @dInstructApprDate is null
    select @dInstructApprDate = instructorapprovaldate
    from SSS_OfferingAcademicPeriods
    where SSS_OfferingsID = @lSSS_OfferingsID
于 2013-09-11T18:18:42.320 回答
1

每个选择只需要一个变量赋值,例如

SELECT  @dInstructApprDate = case when a.InstructorApprovalDate is null  
        then  (select  instructorapprovaldate from SSS_OfferingAcademicPeriods)
....

但更好的是把它分成单独的选择 - 更容易阅读,更容易维护更好的服务器执行,例如

SELECT  @dInstructApprDate = InstructorApprovalDate FROM section

IF @dInstructApprDate IS NULL -- if it's null after first assignmnent
     SELECT  @dInstructApprDate = InstructorApprovalDate FROM SSS_OfferingAcademicPeriods

IF @dInstructApprDate IS NULL -- if it's still null
     SELECT  @dInstructApprDate = TermDate FROM Term
于 2013-09-11T18:19:08.587 回答
1

CASE WHEN ... THEN ... ELSE ... END如下图使用。由于您想要第一次遇到的表中的值,因此您需要先检查IS NOT NULL。如果值是NOT NULL,则CASE语句​​停在那里。

这正是COALESCE工作原理。因此,您可以将CASE语句替换为COALESCE

见演示 -

SQL小提琴

MS SQL Server 2008 架构设置

CREATE TABLE t1 (     id int    , type varchar(50)    );
CREATE TABLE t2 (     id int    , type varchar(50)    );
CREATE TABLE t3 (     id int    , type varchar(50)    );


insert into t1 values (1, 'one');
insert into t1 values (10, null);
insert into t1 values (15, null);
insert into t1 values (20, null);
insert into t2 values (10, 'ten from table t2');
insert into t3 values (15, 'fifteen from table t3');

查询 1

SELECT id,
       CASE WHEN TYPE IS NOT NULL THEN TYPE
       WHEN  (SELECT TYPE FROM t2 WHERE t2.id = t1.id) IS NOT NULL
            THEN (SELECT TYPE FROM t2 WHERE t2.id = t1.id)
       ELSE  (SELECT TYPE FROM t3 WHERE t3.id = t1.id)
       END
FROM t1

结果

| ID |              COLUMN_1 |
|----|-----------------------|
|  1 |                   one |
| 10 |     ten from table t2 |
| 15 | fifteen from table t3 |
| 20 |                (null) |

查询 2

SELECT id, 
      COALESCE(TYPE,
              (SELECT TYPE FROM t2 WHERE t2.id = t1.id),
              (SELECT TYPE FROM t3 WHERE t3.id = t1.id))
FROM t1

结果

| ID |              COLUMN_1 |
|----|-----------------------|
|  1 |                   one |
| 10 |     ten from table t2 |
| 15 | fifteen from table t3 |
| 20 |                (null) |
于 2013-09-11T18:40:43.180 回答