手順で、最初のクエリが null 値を返すかレコードを返さない場合、2 番目のクエリが null 値を返すかレコードを返さない場合でも、2 番目のクエリを実行する必要があり、デフォルト値を返す必要があります。この手順をどのように行うのですか?if else ステートメントまたは例外ハンドラーを使用する必要がありますか?
2784 次
2 に答える
2
これを行う 1 つの方法は、次のように IF ステートメントをネストすることです。
create or replace function get_queue_id
(p_product_code in mo_product_master.product_code%type
, p_intermediary_code in intrfc_intermediary_mstr_view.intermediary_code%type)
return mo_product_master.queue_id%type
as
return_value number;
begin
-- preferred_choice
begin
select pm.queue_id
into return_value
from mo_product_master pm
where pm.product_code=p_product_code
exception
when no_data_found then
null;
end;
if return_value is null then
-- second_choice
begin
select qim.queue_id
into return_value
from mo_queue_inter_map_master qim
, intrfc_intermediary_mstr_view imv
where qim.category_code =imv.category_code
and imv.intermediary_code=p_intermediary_code;
exception
when no_data_found then
null;
end;
if return_value is null then
-- default_value
select id
into return_value
from mo_queue_master
where queue_name='others'
and status='Active';
end if;
end if;
return return_value;
end;
/
それは少し不格好ですが、それは仕事をします.
NO_DATA_FOUND 例外を抑制することは、通常は推奨される方法ではありませんが、このシナリオに適していると思います。最初の QUEUE_ID が見つからないことは、処理が必要な例外ではなく、通常のビジネス ロジックの一部です。例外ハンドラーで後続の選択をネストすることは、ビジネス ルールを表現するほどではないと思います。
于 2011-12-01T14:02:32.527 に答える
0
このようにクエリを書きます
select * from tablename
where field1 in nvl(select field1 from table2 ,'defaultvalue')
于 2011-12-01T12:17:52.830 に答える