Oracle 如何在plsql中从游标传递tablename

Oracle 如何在plsql中从游标传递tablename,oracle,plsql,Oracle,Plsql,我需要对使用游标获取的具有某些条件的所有表进行计数 我无法将游标作为plsql块中的表名传递 代码: create or replace procedure find_tables is cursor tba_tables_cur is select table_name from all tables where owner='ABC'; type tba_tbl is record ( table_name varchar2 (128)); type var_table_name

我需要对使用游标获取的具有某些条件的所有表进行计数

我无法将游标作为plsql块中的表名传递

代码:

create or replace procedure find_tables
is
cursor tba_tables_cur is
    select table_name from all tables where owner='ABC';

type tba_tbl is record ( table_name varchar2 (128));
type var_table_name is table of tba_tbl;
var_table_name_rec var_table_name;
l_array_size number default 1000;
v_count number;

begin

    open tba_tables_cur;
    loop 
    fetch tba_tables_cur into var_table_name_rec limit l_array_size;
    forall i in 1 .. var_table_name_rec.count 
            select count(*) from var_table_name_rec(i) where flag ='Y' into v_count;

    if (v_count >0)
    then
        dbms_output.put_line(v_count);
    end_if;

exit when tba_tables_cur%notfound;
end loop;
close tba_tables_cur;

end;

我面临着一些错误,如无法识别的表名等

您可能需要
立即执行
-

create or replace procedure find_tables
is
  cursor tba_tables_cur is
     select owner, table_name from all_tables where owner='SYS';

  type tba_tbl is record ( owner_name varchar2(128),
                           table_name varchar2 (128));
  type var_table_name is table of tba_tbl;
  var_table_name_rec var_table_name;
  l_array_size number default 1000;
  v_count      number;
  query        varchar2(500);

begin

    open tba_tables_cur;
          loop 
      fetch tba_tables_cur bulk collect into var_table_name_rec limit l_array_size;
      for i in 1 .. var_table_name_rec.count loop
        query := 'select count(*) from ' || 
                  var_table_name_rec(i).owner_name ||
                  '.' ||
                  var_table_name_rec(i).table_name ||
                  ' where flag =''Y''';
        execute immediate query into v_count;

        if (v_count >0) then
          dbms_output.put_line(v_count);
        end if;
       end loop;
  exit when tba_tables_cur%notfound;
  end loop;
  close tba_tables_cur;

end;
/

它一定要这么复杂吗?简单点的怎么样

为了满足“条件”条件,我正在获取包含构成该条件的列的表—
EMPNO
列。那你就

SQL> set serveroutput on;
SQL> declare
  2    l_str varchar2(200); -- to compose a SELECT statement
  3    l_cnt number;        -- contains number of rows in a table
  4  begin
  5    for cur_r in (select c.table_name
  6                  from user_tab_columns c
  7                  where c.column_name = 'EMPNO'
  8                 )
  9    loop
 10      l_str := 'select count(*) from ' || cur_r.table_name ||
 11               ' where empno is not null';    --> that's your "condition"
 12      execute immediate l_str into l_cnt;
 13
 14      dbms_output.put_line(cur_r.table_name ||': '|| l_cnt);
 15    end loop;
 16  end;
 17  /
EMP: 14
BONUS: 0
EMP_1: 3
EMP_2: 5
EMP_3: 6

PL/SQL procedure successfully completed.

SQL>

我面临以下错误。请您协助--Error(17,33):PLS-00597:INTO列表中的表达式“VAR_TABLE_NAME_REC”的类型错误--Error(19,18):PLS-00306:调用“| |”时参数的数量或类型错误。请尝试使用更新的代码。否。不能使用架构名称作为局部变量的前缀,因为它们仅存在于块中。模式前缀将用于引用数据库对象。如果您有一个不同的用户,该用户将作为一个参数并用作“where owner=parameter”。在哪一行?我在查询中添加了架构名称:=“从”| | var|u table_name| rec(I)中选择count(*)。table|name| |“where flag=”Y“;然而,我有循环的问题。游标中大约有2000多个表,而dbmsouput.put_行只打印4个带有计数的表。我已经注释了if条件以查看所有表的计数。我在查询l_str:=“select count(*)from.”| | cur| u r.table|u name | |“其中empno不为null”;然而,我在SQLDeveloper中没有看到任何输出。我已使用我正在使用的连接名称启用dbms输出。系统仅抛出“plsql过程已成功执行”,而不抛出变量输出。我遗漏了什么吗?不要立即执行L_STR,而是第一个DBMS_输出。将L_行(L_STR)放进去,这样您就可以看到实际执行的是什么。然后运行那个SELECT语句,看看它返回了什么(如果有的话)。对不起。我添加了设置serveroutput on;在会议期间,它起了作用。然而,我有循环的问题。游标中大约有2000多个表,而dbmsouput.put_行只打印4个带有计数的表。这意味着只有4个表满足条件。这就是你的
如果(v_count>0)那么…
所说的-仅当你找到某样东西时才显示;如果找不到任何内容,请不要显示任何内容。发布您当前的代码,以便我们查看您拥有的内容。