0

I have the following table (example):

RESEARCH  | START_DATE          | END_DATE            | STATUS         
a         | 2013-11-10 19:00:00 | 2013-11-11 12:00:00 | Ready
b         | 2013-11-12 02:30:00 | 2013-12-01 13:30:00 | Ready
c         | 2013-11-05 07:00:00 | 2013-12-10 15:50:00 | Running

I need to change the status of researches from Ready to Running when START_DATE is reached, and from Running to Finished when END_DATE is reached.

Is there a way of doing this using only SQL Server 2008 R2 Express?

4

2 回答 2

0
UPDATE MyTable 
SET STATUS = 'Running' 
WHERE START_DATE >= CURRENT_TIMESTAMP

UPDATE MyTable 
SET STATUS = 'Finished' 
WHERE END_DATE >= CURRENT_TIMESTAMP

如果您按该顺序运行上面的两个查询,那么您将实现您想要做的事情。

如果您要乱序运行它们,那么第一个 UPDATE 必须是

UPDATE MyTable 
SET STATUS = 'Running' 
WHERE START_DATE >= CURRENT_TIMESTAMP 
AND END_DATE < CURRENT_TIMESTAMP 

或者您可以执行以下操作来组合这两个查询:

UPDATE MyTable 
SET STATUS  =  
CASE  
  WHEN START_DATE >= CURRENT_TIMESTAMP 
     AND END_DATE < CURRENT_TIMESTAMP  THEN 'Running' 
  WHEN END_DATE >= CURRENT_TIMESTAMP THEN 'Finished'
  ELSE 'Ready'
  END 
于 2013-11-06T18:15:40.820 回答
0

我认为这样的事情对你有用:

update Table 
set 
    Status = case 
        when 
            CURRENT_TIMESTAMP  between START_DATE and END_DATE 
            and STATUS = 'Ready' then 'Running'
        when 
            CURRENT_TIMESTAMP  > END_DATE 
            and STATUS = 'Running' then 'Finished'
        else
            Status
        end;

当然,在开始更新之前,您必须确保状态正常。

于 2013-11-06T18:22:27.247 回答