0

我有一个使用 case 语句的存储过程,如下所示:我要做的是评估 testTable 中的 2 列的日期。所以下面的 case 语句说如果 stop_date 为 null 或大于当前日期,则设置 is_active cloumn 为 Y else N

我要做的也是评估另一个日期列,例如 another_stop_date 并检查它是否为空或日期大于今天,并使用相同的逻辑来更新 is_active 列

我不确定我们是否可以使用多个 case 语句逻辑来更新单个列?

我在下面的代码中评论了我没有得到正确结果的地方

基本上需要从 testTable 评估 stop_dt 和 another_stop_date 列!

USE [Test]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROC [dbo].[p_test]
@Blank_Row CHAR(1) = 'N'

AS
BEGIN

SET NOCOUNT ON

DECLARE @TD  DATETIME

SELECT @TD = GETDATE()

DECLARE @tempTable TABLE (

ID INT,
c_id   INT,
desc varchar(40),
date datetime,
s_col  TinyINT,
is_active char(1),
stuff VARCHAR(8))


 INSERT INTO @tempTable

 SELECT id, c_id, desc, max( date ), 1,

 CASE WHEN (stop_dt IS NULL OR stop_dt > @TD) THEN 'Y' 
 --//Case When (another_stop_date is NULL or another Stop_date > @TD) THEN 'Y'<-----confused here
 ELSE 'N' END,

 stuff

 FROM testTable

 GROUP BY id, stop_dt, c_id, desc, stuff, another_stop_date

 Select * from tempTable
4

2 回答 2

3

您可以将 case 语句中的子句与通常的逻辑运算符组合在一起,也可以使用单独的 case:

Case 
    When 
        (stop_dt is null or stop_dt > @td) and
        (another_stop_date is null or another_stop_date > @td) 
    Then 'Y' 
    Else 'N' 
End
于 2013-09-10T13:51:14.237 回答
0

Case 语句的操作接近于 if 语句,并且可以有多个子句。

Case when condition_1 > 1 then 'hi'
when condition_1 < 14 then 'no'
when condition_89 > 12 then 'why is this here'
else 1
end

将其应用于您的陈述:

 CASE WHEN (stop_dt IS NULL OR stop_dt > @TD) THEN 'Y' 
 When (another_stop_date is NULL or another Stop_date > @TD) THEN 'Y'<-----confused here
 ELSE 'N' END
于 2013-09-09T21:25:49.440 回答