我有下表(示例):
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
我需要在达到START_DATE时将研究状态从Ready变为Running,并在达到END_DATE时从Running to Finished更改。
有没有办法只使用SQL Server 2008 R2 Express?
答案 0 :(得分: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
答案 1 :(得分: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;
当然,在开始更新之前,您必须确保状态正常。