简体   繁体   中英

Evaluate no result condition in SQL CASE

I would like to use the SQL CASE keyword to evaluate the result of an inner query, in particular to check whether there is no record:

PseudoCode:

UPDATE tableA AS outerTable
SET field = CASE 
                WHEN (
                         SELECT * 
                         FROM tableB 
                         WHERE innerField = outerTable.field
                     ) IS [doesn't have result]
                THEN (
                        ...
                     ) 
                ELSE "default"
            END 

How can I code this condition? I've tried with IS NULL but it doesn't seem to work ( Scalar subquery produced more than one element)

You are looking for NOT EXISTS :

WHEN NOT EXISTS (SELECT * 
                 FROM tableB b
                 WHERE b.innerField = outerTable.field
                ) 
THEN . . .
            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM