3

我在 oracle 11g 服务器中有一个存储过程,它有一个 out 记录变量。我无法编辑此过程。我正在创建一个函数,它将调用该过程并返回记录集中的信息。我看了这里问的以下问题: 过去的问题

我的问题是我可以为记录创建一种表并直接在 SQL 中查询吗?还是我需要将记录转换为类型对象并创建表类型以供其直接查询?

4

5 回答 5

3

RECORD 是一个 PL/SQL 概念。因此,我们不能基于 RECORD 创建 TABLE TYPE 并在 SQL 语句中使用该 TYPE。SQL 只识别在 SQL 中创建的 TYPE。

所以,如果我正确理解了你的场景,你需要在 SQL 中创建一个对象和/或一个嵌套表。然后您的函数可以调用该过程并将 RECORD 转换为嵌套表。然后可以返回或流水线化嵌套表。

于 2009-08-04T14:57:30.820 回答
1

您的链接指向的问题已标记为Oracle,所以我假设这就是您正在使用的问题。

最简单的方法可能是返回一个CURSOR

SQL> VAR cr_dual REFCURSOR

SQL> 开始
  2 打开:cr_dual FOR
  3 选择 1
  4 来自双
  5 联合所有
  6 选择 2
  7 从双;
  8 结束;
  9 /

Процедура PL/SQL успешно завершена。

SQL> 打印 cr_dual

         1
----------
         1
         2
于 2009-06-30T17:53:34.573 回答
1

最终解决这个问题的是基于存储过程逻辑创建一个视图并直接从中查询。

于 2011-04-12T23:03:16.127 回答
0

if you are receiving only one record and you know the record definition, you should be able to the field of the record directly by name.

for example:

TYPE myrecord IS RECORD (
       itemA NUMBER,
       itemB myTable.columnName%TYPE
  );

when you have a record of myrecord type, you can reference itemA in a manner like this:

CREATE OR REPLACE FUNCTION myFunction(theRecord IN myrecord%TYPE) RETURN NUMBER
IS
BEGIN    
   RETURN recordInstance.itemA;
END myFunction;

note that you may be able to get around this in your calling code be simply handling the record type that the original procedure returns.

于 2009-06-30T18:08:13.193 回答
0

你可以试试可变数组。

第一的 -

create or replace type addr_type
as object
(name   varchar2(20)
,city   varchar2(20)
)

并将您的可变数组创建为

create or replace type varr_addr as varray(10) of addr_type
/

现在您可以使用 varr_addr 返回工作。一个例子:

SQL> create or replace procedure ret_user_addr (p_out out varr_addr)
  2  is
  3  begin
  4    p_out := varr_addr(addr_type('NAME1','CITY1'),addr_type('NAME2','CITY2'));
  5  end;
  6  /

Procedure created.

SQL> sho err
No errors.

现在您需要从调用的位置正确配置 out 变量。您可以像您一样从 table(VARIABLE_NAME) 中选择。

于 2009-07-14T14:06:19.503 回答