案例陈述中的案例

时间:2013-09-11 18:11:49

标签: sql sql-server-2008 case

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

如果所有三个都为null,那么我需要以与instructorapprovaldate相同的方式获得absoluteExpireDate。

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

如何创建一个case语句来处理它?<​​/ p> 到目前为止,这就是我所拥有的:

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

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:1)

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

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

你的代码看起来像这样(伪):

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

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

答案 3 :(得分:1)

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

对我而言,这比案例陈述更清晰/更容易阅读,我怀疑它也会表现得更好。

基于代码&amp;您的问题中包含的说明,对于您来说,这看起来类似于以下内容:

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

答案 4 :(得分:1)

使用CASE WHEN ... THEN ... ELSE ... END,如下所示。由于您需要首次遇到的表中的值,因此需要先检查IS NOT NULL。如果值为NOT NULL,则CASE语句将停止。

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

参见演示 -

SQL Fiddle

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

<强> Results

| 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

<强> Results

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