游标嵌套使用的问题

jinweifu 2006-12-10 07:17:21
create or replace procedure MULTIPLE_CURSORS_PROC is
v_owner varchar2(40);
v_table_name varchar2(40);
v_column_name varchar2(100);

/* First cursor */
cursor get_tables is
select distinct tbl.owner, tbl.table_name
from all_tables tbl
where tbl.owner = 'SYSTEM';

/* Second cursor */
cursor get_columns is
select distinct col.column_name
from all_tab_columns col
where col.owner = v_owner
and col.table_name = v_table_name;

begin

-- Open first cursor
open get_tables;
loop
fetch get_tables into v_owner, v_table_name;

open get_columns;
loop
fetch get_columns into v_column_name;

end loop;
close get_columns;

end loop;
close get_tables;

EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
end MULTIPLE_CURSORS_PROC;


没有发现哪里相关 很不好理解。我的理解是这样的:
首先获得all_tables中owner='SYSTEM'的子集,存入v_owner,v_table_name;
接着获取,接着循环抓取all_tab_columns里的若owner=all_tables.owner并且table_name=all_tables_name则column_name存入v_column_name中,是不是这样的解的,还有上面的
DISTINCT,select distinct tbl.owner, tbl.table_name
from all_tables tbl
where tbl.owner = 'SYSTEM';
是要求tbl.owner与tbl.table_name同时DISTINCT吗?即:
用下面的语句:
SELECT distinct tbl.owner,tbl.table_name from tbl;


table.owner tbl.table_name
A A1
A A2
B B1
B A1

这样四个是不是会全部选出来?




...全文
215 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinweifu 2006-12-15
  • 打赏
  • 举报
回复
thx
wiler 2006-12-11
  • 打赏
  • 举报
回复
try:
/* Second cursor */
cursor get_columns is
select distinct col.column_name
from all_tab_columns col
where col.owner = :1
and col.table_name = :2;
......
open get_columns using v_owner, v_table_name;
tianyacao007 2006-12-10
  • 打赏
  • 举报
回复
会全出来的!

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧