oracle - Query in WHEN NO_DATA_FOUND clause. Bad practice? -
developers @ our job using follow construct:
begin select field1 l_field1 table1 field2 = 'some value' exception when no_data_found select field2 l_field2 table2 field3 = 'some value' ... end; what means - if don't find record in first table go in second table.
i have strong feeling wrong , bad lazy practice. how approach it? first count in table1 , if number of rows 0 go , in table2?
if asking alternative way, can use below way. when no data found considered handle errors or warning, , not conditions. exceptions helps handle errors , if trying run several selects , had error .the exception detect check below example.
begin declare cnt number(4); select count(1) cnt table1 field2 = 'some value'; if cnt >0 select field1 l_field1 table1 field2 = 'some value' else if cnt =0 select field2 l_field2 table2 field3 = 'some value'; end if; end; with exceptions, can handle errors conveniently without need code multiple checks, follows:
begin select ... select ... select ... ... exception when no_data_found -- catches 'no data found' errors for more information exception , handling errors check oracle documentation
Comments
Post a Comment