5

2 つのテーブル (従業員と部門) を使用して関数から %rowtype で複数の値を返そうとしていますが、うまくいきません。

create or replace function get_employee
 (loc in number)
return mv_emp%rowtype
as  
   emp_record mv_emp%rowtype;
begin
   select a.first_name, a.last_name, b.department_name into emp_record 
   from employees a, departments b 
   where a.department_id=b.department_id and location_id=loc;

   return(emp_record);  
end;
4

3 に答える 3

10

上記の関数はエラーなしでコンパイルされましたか? の型はMV_EMP何ですか? 理想的には、以下のようなものであるべきです。

create or replace type emp_type
(
first_name varchar2(20)
, last_name varchar2(20)
, depart_name varchar2(20)
)
/
create or replace function get_employee
 (loc in number)
return emp_type
as  
   emp_record emp_type;
begin
   select a.first_name, a.last_name, b.department_name into emp_record 
   from employees a, departments b 
   where a.department_id=b.department_id and location_id=loc;

   return(emp_record);  
end;
于 2012-05-31T21:43:58.797 に答える
1
create type t_row as object (a varchar2(10));

create type t_row_tab as table of t_row;

入力文字列を分割する関数を作成します。

create or replace function get_number(pv_no_list in varchar2) return t_row_tab is
lv_no_list t_row_tab := t_row_tab();
begin

  for i in (SELECT distinct REGEXP_SUBSTR(pv_no_list, '[^,]+', 1, LEVEL) no_list FROM dual
  CONNECT BY REGEXP_SUBSTR(pv_no_list, '[^,]+', 1, LEVEL) IS NOT NULL)
  loop
  lv_no_list.extend;
  lv_no_list(lv_no_list.last) := t_row(i.no_list);
  end loop;

  return lv_no_list;

end get_number;

関数が配置されると、SQL ステートメントの table 句を使用して目的の結果を得ることができます。必要に応じて、関数から複数の値が返されました。

SQL> select * from table(get_number('1,2,3,4'));


A
----------
1
3
2
4

これで、関数は単純にテーブルのように動作します。これらのカンマ区切りの値を "IN" 句の一部にする必要がある場合があります。

例えば ​​:

select * from dummy_table where dummy_column in ('1,2,3,4');

ただし、「1,2,3,4」は文字列であり、個々の数値ではないため、上記のクエリは機能しません。この問題を解決するには、次のクエリを使用するだけです。

select * from dummy_table where dummy_column in ( select * from table(get_number('1,2,3,4')) );

参照: http://www.oraclebin.com/2012/12/returning-multiple-values-from-function.html

于 2013-01-03T17:06:20.107 に答える