I have a query that needs to check that all fields have values are in a list of valid codes. Right now I’m calling the same subquery over and over and over again. I want to abstract the subquery out so that it is faster and the code isn’t repeated. This is the query in question:
select count(*)
into cnt
from pdv_validcodes c
where c.code_type = 'YNNA'
and (upper(:new.spec_1) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_1 is null)
and (upper(:new.spec_2) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_2 is null)
and (upper(:new.spec_3) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_3 is null)
and (upper(:new.spec_4) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_4 is null)
and (upper(:new.spec_5) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_5 is null)
and (upper(:new.spec_6) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_6 is null)
and (upper(:new.spec_7) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_7 is null)
and (upper(:new.spec_8) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_8 is null)
and (upper(:new.spec_9) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_9 is null)
and (upper(:new.spec_10) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_10 is null)
and (upper(:new.add_spec_1) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_1 is null)
and (upper(:new.add_spec_2) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_2 is null)
and (upper(:new.add_spec_3) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_3 is null)
and (upper(:new.add_spec_4) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_4 is null)
and (upper(:new.add_spec_5) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_5 is null);
Michael,
I haven’t had a chance to test this but as it’s trigger code and therefore PL/SQL, something along the lines of this might work: