+ -
当前位置:首页 → 问答吧 → 如果在一个游标内部再定义一个新的游标

如果在一个游标内部再定义一个新的游标

时间:2011-09-23

来源:互联网

codes......

cursor t_name is
  select table_name
  from user_tab_cols
  where column_name = 'AAC001'
  and (length(table_name) = 4 or length(table_name) = 5)
  and table_name not like 'S%'
  and table_name not like '%AC01%';

codes...
open t_name;
  loop
  fetch t_name
  into v_tname;
  exit when t_name%notfound;
cursor t_cname is
select column_name from user_tab_cols where table_name=v_tname;
codes...
end loop;
codes...



分割线
==================================================================================
上段代码中的红色部分编译的时候报错,那应该怎么改才正确呢?!求高人指点!!

作者: lwlmj2008   发布时间: 2011-09-23

亲,在ORACLE中,CURSOR的声明或者定义,是不能出现的实际的代码段中的。
还是得放到codes前面。

在codes里,直接使用就可以了

作者: yixilan   发布时间: 2011-09-23

set serveroutput on;
declare
  v_tname varchar2(100) := '';
  c_tname varchar2(100) := '';
  cursor t_name is
  select table_name from user_tab_cols where column_name = 'T3';
  
  cursor t_cname is
  select column_name from user_tab_cols where table_name = v_tname;

begin
open t_name;
loop
  fetch t_name into v_tname;
  exit when t_name%notfound;
  open t_cname;
  loop fetch t_cname into c_tname;
  dbms_output.put_line(c_tname);
  exit when t_cname%notfound;
  end loop;
  close t_cname;
end loop;
close t_name;

end;

作者: yixilan   发布时间: 2011-09-23

你这有必要声明两个游标吗?
SQL code

declare
    cursor c_tab_col is
        select table_name, column_name
          from user_tab_cols
         where column_name = 'AAC001'
           and (length(table_name) = 4 or length(table_name) = 5)
           and table_name not like 'S%'
           and table_name not like '%AC01%';
begin
    for tab in c_tab_col
    loop
        -- 代码中获取表名使用 tab.table_name
        -- 获取列名 tab.column_name
    end loop;
end;

作者: opps_zhou   发布时间: 2011-09-23

热门下载

更多