Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

db2 - Update statement using subselect with case

Tags:

db2

I want to do an update statement according to a result of a subquery

For example :

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;

So could you please advice me how can I do it?

I tried the following but I got a syntax error :

SELECT count(*) as TC
 CASE
   WHEN TC > 0 
   THEN '1'
   ELSE '0'
   END  AS dum
   FROM Event E where E.Type= 'CANCELLING';
like image 314
User Avatar asked Jun 27 '26 00:06

User


1 Answers

CASE is perfectly suitable:

UPDATE TABLE1
SET A = 
CASE 
   WHEN (SELECT count(*) FROM TABLE2) > 0 THEN 1
   ELSE 0
END
like image 65
Anatolii Gabuza Avatar answered Jun 28 '26 18:06

Anatolii Gabuza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!