0

在一个过程中,如果我的第一个查询返回空值或不返回任何记录,那么我的第二个查询必须运行,即使第二个查询返回空值或不返回任何记录,则必须返回默认值。如何制作这个程序?我应该使用 if else 语句还是异常处理程序?

4

2 回答 2

2

这样做的一种方法是嵌套 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;
/

它有点笨重,但它可以完成工作。

Suppressing the NO_DATA_FOUND exception is not usually recommended practice but I think it fits this scenario: not finding the first QUEUE_ID is part of the regular business logic rather than an exception which needs to be handled. I don't think nesting the subsequent selects in the exception handler is nearly as expressive of the business rules.

于 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 回答