say i have the following function:
function xx_func
return varchar2
is
l_value varchar2(1);
begin
select 'Y'
into l_value
from func_table
where lookup = 'hello';
RETURN l_value;
EXCEPTION
WHEN NO_DATA_FOUND THEN
l_value := 'N';
RETURN l_value;
end xx_func;
Now, in the current PL/SQL code, we have this:
DECLARE
l_optin varchar2(100);
t_col1 varchar2(100);
BEGIN
l_optin := xx_func;
if l_optin = 'Y' then
select col1
into t_col1
from xx_table_1 xx1
, xx_table_2 xx2
where 1=1
and xx1.t_col2 = xx2.t_col2;
else
select col1
into t_col1
from xx_table_1 xx1
, xx_table_2 xx2
where 1=1
and xx1.t_col2 = xx2.t_col2
and xx1.t_col3 = xx2.t_col3;
end if;
END;
I wanted to code it like this to make it shorter:
DECLARE
l_optin varchar2(100);
t_col1 varchar2(100);
BEGIN
l_optin := xx_func;
select col1
into t_col1
from xx_table_1 xx1
, xx_table_2 xx2
where 1=1
and xx1.t_col2 = xx2.t_col2
and ((xx1.t_col3 = xx2.t_col3 and l_optin = 'N')
OR (l_optin = 'Y'));
END;
The code above tells me If l_optin is 'N' then evaluate xx1.t_col3 = xx2.t_col3. if l_optin = 'Y', then don't evaluate xx1.t_col3 = xx2.t_col3.
I tried testing it locally and it works but my co-worker says it won't work on all scenarios. The columns are not nullable.
Are there any possible scenarios that this wouldn't handle?
Note:
- We cannot use Dynamic SQL
- this sample code occurs multiple times in the package of over 14000 lines, hence, it's quite hard to read at that point.
1=1as part of theWHEREclause, AFAIK that's only useful if you are already using dynamic SQL and need to ensure a valid where clause to append to.ORin the second version is misleadingly placed and inconsistently capitalised.xx_funcfunction into single SQL (i.e.... from xx_table_1 xx1 join xx_table_2 xx2 on xx1.t_col2 = xx2.t_col2 left join func_table ft on ft.lookup = 'hello' where (ft.lookup is not null or xx1.t_col3 = xx2.t_col3)). Anyway I finally consider its readability disputable - the ANSI syntax is suitable for join chains of two tables at a time, here we actually have three tables, all involved in join condition in one go - and from performance point of view it is not clear winner too.