当我提供i / p时执行函数[参数不在表格中我得到错误;否则它被执行]

时间:2014-05-20 10:48:36

标签: plsql

create or replace function get_ware_house_master(p_WAREHOUSE_IDS in  varchar2)
     return id_wh_id

is
    l_warehouse_list id_wh.id := id_wh_id();
    str varchar2(300);        
begin

    str := 'SELECT BM(w.wh_id)    
            FROM pod_place_warehouse_mapping_tb W
            where ( W.wh_id  IN  (' ||p_WAREHOUSE_IDS  || '))';

    execute immediate str bulk collect into l_warehouse_list;

    for i in l_warehouse_list.first..l_warehouse_list.last loop  
        dbms_output.put_line(l_warehouse_list(i).wh_id);    
    end loop;    

    return l_warehouse_list;
end get_ware_house_master;

如果l_warehouse_list = null然后dbms_output('No DATA FOUND')但是如果我在下面的函数中提到的表中没有数据错误,那么如何处理我需要使用的表中不存在的数据错误:< / p>

  

numeric or value change error

1 个答案:

答案 0 :(得分:0)

你可以在你的那个集合上使用count()函数:

create or replace function get_ware_house_master(p_WAREHOUSE_IDS in  varchar2)
     return id_wh_id
is
    l_warehouse_list id_wh.id := id_wh_id();
    str varchar2(300);        
begin

    str := 'SELECT BM(w.wh_id)    
            FROM pod_place_warehouse_mapping_tb W
            where ( W.wh_id  IN  (' ||p_WAREHOUSE_IDS  || '))';

    execute immediate str bulk collect into l_warehouse_list;

    if l_warehouse_list.count() >0 then
      for i in l_warehouse_list.first..l_warehouse_list.last loop  
        dbms_output.put_line(l_warehouse_list(i).wh_id);    
      end loop;    
    else
        dbms_output.put_line('No data');
    end if;

    return l_warehouse_list;
end get_ware_house_master;

以下是如何通过简化代码来说明最新内容:

CREATE OR REPLACE TYPE id_wh_id AS TABLE OF VARCHAR2(100);

create or replace function get_ware_house_master(p_WAREHOUSE_IDS in  varchar2)
     return id_wh_id

is
    l_warehouse_list id_wh_id := id_wh_id();
    str varchar2(300);        
begin

    str := 'SELECT * from dual where ''a''=''' ||p_WAREHOUSE_IDS  || '''';
        dbms_output.put_line(str);  
    execute immediate str bulk collect into l_warehouse_list;


if l_warehouse_list.count() >0 then
    for i in l_warehouse_list.first..l_warehouse_list.last loop  
        dbms_output.put_line(l_warehouse_list(i));    
    end loop;    
else
    dbms_output.put_line('No data');
end if;

    return l_warehouse_list;
end get_ware_house_master;
/

-- and call to the function:
          select get_ware_house_master('sbi') from dual
union all select get_ware_house_master('a')   from dual
;