db2 - 使用带有大小写的subselect更新语句

时间:2014-01-02 13:51:29

标签: db2

我想根据子查询的结果执行更新语句

例如:

Update TABLE1 
set A= (Select Count(*) from TABLE2 ) 
if the value of count is 0 then Update the value of A to be 0 Else set A = 1;

那么请你建议我怎么做?

我尝试了以下操作但是语法错误:

SELECT count(*) as TC
 CASE
   WHEN TC > 0 
   THEN '1'
   ELSE '0'
   END  AS dum
   FROM Event E where E.Type= 'CANCELLING';

2 个答案:

答案 0 :(得分:5)

CASE非常合适:

UPDATE TABLE1
SET A = 
CASE 
   WHEN (SELECT count(*) FROM TABLE2) > 0 THEN 1
   ELSE 0
END

答案 1 :(得分:1)

declare @count int
set @count=Select Count(*) from TABLE2 

if @count=0
BEGIN
Update TABLE1 
set A=0
end
else
Update TABLE1 
set A=1