我正在尝试在oracle中创建一个返回多行的存储函数。
我的问题与this one非常相似,只是我想要提取select *
查询
简而言之,我想创建一个返回此查询结果的函数
select * from t_email_queue
我试过的是:
create or replace
PACKAGE email_queue AS
type t_email_queue_type is table of T_EMAIL_QUEUE%ROWTYPE;
FUNCTION lock_and_get return t_email_queue_type;
END email_queue;
create or replace
PACKAGE BODY email_queue AS
FUNCTION lock_and_get RETURN t_email_queue_type AS
queue_obj t_email_queue_type;
cursor c (lockid in varchar2) is select * from t_email_queue where lockedby = lockid;
lockid varchar2(100) := 'alf';
BEGIN
OPEN c(lockid);
FETCH c bulk collect INTO queue_obj;
return queue_obj;
END lock_and_get;
END email_queue;
包编译得很好,但是当我尝试使用此查询调用它时
select * from table(email_queue.lock_and_get);
Oracle抛出以下错误
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Error at Line: 1 Column: 20
我认为Oracle希望我在架构级别创建我的返回类型,但是当我尝试
时create type t_email_queue_type is table of T_EMAIL_QUEUE%ROWTYPE;
Oracle抱怨
Type IMAIL.T_EMAIL_QUEUE_TYPE@imail dev
Error(1): PL/SQL: Compilation unit analysis terminated
Error(2,37): PLS-00329: schema-level type has illegal reference to IMAIL.T_EMAIL_QUEUE
有人能指出我正确的方向吗?我在这里缺少什么?
感谢阅读!
答案 0 :(得分:4)
使用SQL类型,你不能做%ROWTYPE,你必须键入每一列以匹配表*。
* sys.anydataset放在一边。但是沿着这条路走下来会有更复杂的编码。
e.g。如果你的表是
create table foo (id number, cola varchar2(1));
然后
create type email_queue_type is object (id number, cola varchar2(1));
/
create type t_email_queue_type as table of email_queue_type;
/
并使用该表email_queue_type_tab作为函数的输出。
但我建议使用流水线功能,因为您当前的代码不可扩展。
例如:
SQL> create table foo (id number, cola varchar2(1));
Table created.
SQL>
SQL> create type email_queue_type is object (id number, cola varchar2(1));
2 /
Type created.
SQL> create type t_email_queue_type as table of email_queue_type;
2 /
Type created.
SQL> insert into foo select rownum, 'a' from dual connect by level <= 10;
10 rows created.
SQL>
SQL> create or replace PACKAGE email_queue AS
2
3
4 FUNCTION lock_and_get return t_email_queue_type pipelined;
5
6 END email_queue;
7 /
Package created.
SQL> create or replace PACKAGE BODY email_queue AS
2
3 FUNCTION lock_and_get RETURN t_email_queue_type pipelined AS
4 queue_obj t_email_queue_type;
5
6 BEGIN
7
8 for r_row in (select * from foo)
9 loop
10 pipe row(email_queue_type(r_row.id, r_row.cola));
11 end loop;
12
13 END lock_and_get;
14
15 END email_queue;
16 /
Package body created.
SQL> select * from table(email_queue.lock_and_get());
ID C
---------- -
1 a
2 a
3 a
4 a
5 a
6 a
7 a
8 a
9 a
10 a
10 rows selected.
SQL>
答案 1 :(得分:4)
如果您不是特别热衷于使用SQL类型,则可以使用sys_refcursor
代替:
create or replace package email_queue as
function lock_and_get return sys_refcursor;
end email_queue;
/
create or replace package body email_queue as
function lock_and_get return sys_refcursor AS
c sys_refcursor;
lockid varchar2(100) := 'alf';
begin
open c for
select * from t_email_queue
where lockedby = lockid;
return c;
end lock_and_get;
end email_queue;
/
从SQL * Plus中你可以称之为:
var cur refcursor;
exec :cur := email_queue.lock_and_get;
print cur
并且exec
是简单匿名块的简写,您也可以从其他PL / SQL对象调用。但是你不能做的是:
select * from table(email_queue.lock_and_get);
我不熟悉从PHP调用函数,但是从Java可以直接使用它作为可调用语句的返回,因此根本不需要select * from table()
构造。我不知道你是否可以在PHP调用中执行一个匿名块,比如begin $cur = email_queue.lock_and_get; end;
,并且$cur
作为你的结果集然后可以迭代通过?
我意识到这不是一个完整的答案,因为PHP方面过于模糊,但可能会给你一些想法。
答案 2 :(得分:1)
如果您正在使用PHP,并且要访问oracle存储函数。您可以使用类似这样的
//Your connection details
$conn = oci_connect($username, $password, '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))' );
/* Your query string; you can use oci_bind_by_name to bind parameters or just pass the variable in it*/
$query = "begin :cur := functionName('".$param1."','".$param2."','".$param3."'); end;";
$stid = oci_parse($conn, $query);
$OUTPUT_CUR = oci_new_cursor($conn);
oci_bind_by_name($stid, ':cur', $OUTPUT_CUR, -1, OCI_B_CURSOR);
oci_execute($stid);
oci_execute($OUTPUT_CUR);
oci_fetch_all($OUTPUT_CUR, $res);
// To get your result
var_dump($res);
我希望这会有所帮助。