Best way to structure a PL / SQL IF THEN statement?

Just wondering if there is a better way to write the following PL / SQL code in ORACLE?

IF(p_c_courtesies_cd is not null 
OR  p_c_language_cd is not null
OR v_c_name is not null
OR v_c_firstname is not null
OR v_c_function is not null
OR p_c_phone is not null
OR p_c_mobile is not null
OR p_c_fax is not null
OR v_c_email is not null
) THEN
     -- Do something
END IF;
+3
source share
4 answers
If coalesce( expr1, expr2, ... expr_n ) is not null then do something end if;

See here.

(Thanks to Tony for the correction)

+9
source

My answer will be a simple no.

Although there are several alternative ways of writing the same construct, I donโ€™t think they are necessarily โ€œbetterโ€.

Anyone can look at the IF statement and know exactly what that means. Alternatives based on concatenation or using a coalescence operator simply hide the intention.

+2
source

coalesce (expr1, expr2,... exprn) , ...

+1

In another way, using the fact that Oracle treats NULL and '' as one and the same:

IF p_c_courtesies_cd 
   || p_c_language_cd 
   || v_c_name 
   || v_c_firstname 
   || v_c_function 
   || p_c_phone 
   || p_c_mobile p_c_fax 
   || v_c_email is not null
THEN
     -- Do something
END IF;
0
source

All Articles