【问题标题】:if my first query returns null value then my second query has to run even the second query is null then my default value as to shown如果我的第一个查询返回空值,那么即使第二个查询为空,我的第二个查询也必须运行,那么我的默认值如图所示
【发布时间】:2012-01-10 14:04:38
【问题描述】:

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

【问题讨论】:

    标签: oracle stored-procedures plsql oracle10g plsqldeveloper


    【解决方案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 是常规业务逻辑的一部分,而不是需要处理的异常。我不认为在异常处理程序中嵌套后续选择几乎可以表达业务规则。

    【讨论】:

      【解决方案2】:

      这样写你的查询

      select * from tablename 
        where field1 in nvl(select field1 from table2 ,'defaultvalue')
      

      【讨论】:

      • 我收到此错误 ORA-00936:在我尝试执行此查询时缺少表达式 SELECT pm.queue_id FROM mo_product_master pm WHERE pm.product_code=:product_code AND pm.queue_id in is NULL (SELECT qim.queue_id FROM mo_queue_inter_map_master QIM, intrfc_intermediary_mstr_view IMV WHERE QIM.CATEGORY_CODE =imv.category_code AND imv.intermediary_code=:intermediary_code, 从 mo_queue_master 选择 id where QUEUE_NAME='others' and status='Active');
      • oracle 没有 isnull() 函数,你必须在 isull 的地方使用 nvl()
      • 我在这里仍然没有得到我想要的结果我的 sql 语句 MyFirstQuery 是 SELECT pm.queue_id FROM mo_product_master pm WHERE pm.product_code=:product_code;如果我的第二个查询没有返回任何记录,那么我的第二个查询输出应该显示 SELECT qim.queue_id FROM mo_queue_inter_map_master QIM, intrfc_intermediary_mstr_view IMV WHERE QIM.CATEGORY_CODE =imv.category_code AND imv.intermediary_code=:intermediary_code;如果我之前的两个查询都未能返回记录,那么我的第三个查询输出应该显示 select id from mo_queue_master where QUEUE_NAME='others' and status='Active';
      猜你喜欢
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2018-12-01
      • 2015-03-16
      • 2018-12-18
      • 2021-02-27
      相关资源
      最近更新 更多