首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从oracle存储过程中返回动态游标

问如何从oracle存储过程中返回动态游标
EN

Stack Overflow用户
提问于 2013-01-08 17:35:55
回答 1查看 2K关注 0票数 3

我有两个表,其中ID字段是常见的。

我正在一个游标中获取第一个表的所有记录。然后我想要做的是,根据游标中的每个ID,我想从第二个表中获取值,然后返回该值。

我怎么才能做到这一点。请帮帮我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-08 17:59:41

家庭作业?

这是基本的SQL。通常情况下,您将连接这两个表。

代码语言:javascript
复制
begin
  for r_row in (select b.*
                  from tab1 a 
                       inner join tab2 b
                               on b.id = a.id)
  loop
    null; -- do whatever
  end loop;
end;
/

如果您有一个现有的游标,但不能更改它

例如,your_cursor只返回一个ID列。

代码语言:javascript
复制
begin
  open your_cursor;
  loop
    fetch your_cursor into v_id;
    exit when your_cursor%notfound;
    for r_row in (select * from tab2 b where b.id = v_id)
    loop
      null; -- do whatever here.
    end loop;
  end loop;
end;
/

编辑:根据评论:

以下是一些示例数据:

代码语言:javascript
复制
SQL> create table table1 (id number primary key, name varchar2(20));

Table created.

SQL> create table table2 (id number, col1 varchar2(20), col2 varchar2(20));

Table created.

SQL> insert into table1 values (1, 'test');

1 row created.

SQL> insert into table1 values (2, 'foo');

1 row created.

SQL>
SQL> insert into table2 values (1, 'John', 'Smith');

1 row created.

SQL> insert into table2 values (1, 'Peter', 'Jones');

1 row created.

SQL> insert into table2 values (1, 'Jane', 'Doe');

1 row created.

SQL> insert into table2 values (2, 'Nina', 'Austin');

1 row created.

SQL> insert into table2 values (2, 'Naman', 'Goyal');

1 row created.

SQL> commit;

Commit complete.

创建一个类型来保存返回结构。注数据类型需要与表table1和table2的数据类型匹配(%type不起作用,因此请确保它们匹配)

代码语言:javascript
复制
SQL> create type my_obj as object (
  2    id number,
  3    name varchar2(20),
  4    col1 varchar2(20),
  5    col2 varchar2(20)
  6  );
  7  /

Type created.

SQL> create type my_tab as table of my_obj;
  2  /

Type created.

现在创建您的函数(如果在您的实际代码中,您可以将其放入包中,则可以这样做)。

代码语言:javascript
复制
SQL> create function function1
  2    return my_tab pipelined
  3  is
  4  begin
  5    for r_row in (select t1.id, t1.name, t2.col1, t2.col2
  6                    from table1 t1
  7                         inner join table2 t2
  8                                 on t1.id = t2.id)
  9    loop
 10      pipe row(my_obj(r_row.id, r_row.name, r_row.col1, r_row.col2));
 11    end loop;
 12  end;
 13  /

Function created.

SQL>
SQL> select *
  2    from table(function1);

        ID NAME                 COL1                 COL2
---------- -------------------- -------------------- --------------------
         1 test                 John                 Smith
         1 test                 Peter                Jones
         1 test                 Jane                 Doe
         2 foo                  Nina                 Austin
         2 foo                  Naman                Goyal

如果需要,您可以将输入传递到该函数中,例如table(function1('a', 'b'));等。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14211972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档