Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 查询以查找我的架构名称HR中的空表数_Oracle - Fatal编程技术网

Oracle 查询以查找我的架构名称HR中的空表数

Oracle 查询以查找我的架构名称HR中的空表数,oracle,Oracle,我想在我的模式hr中找到空表的数量,并为此编写了代码: set serveroutput on; Declare d Number; c varchar2(25); cursor c1 is SELECT DISTINCT OWNER, OBJECT_NAME FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OWNER = 'HR'; Begin for r in c1 Loop select count(*) into d from (

我想在我的模式hr中找到空表的数量,并为此编写了代码:

set serveroutput on;


Declare
d Number;
c varchar2(25);
cursor c1 is SELECT DISTINCT OWNER, OBJECT_NAME FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OWNER = 'HR';

Begin
 for r in c1
Loop
select count(*) into d
from (r.object_name);
if (d = 0) then
dbms_output.put_line(r.object_name||'is Empty');
end if;
end loop;
end;
但是它在(r.object_name)中的这一行抛出错误。有人能帮我吗?

您需要(
立即执行
)来实现:

declare
  d number;
  v_sql varchar2(1000);

  cursor c1 is select object_name 
    from all_objects 
    where object_type = 'TABLE' and owner = 'HR';

begin
  for r in c1 loop
      v_sql := 'select count(*) from HR.'||r.object_name;
      execute immediate v_sql into d;
      if d = 0 then
          dbms_output.put_line(r.object_name||' is empty');
      end if;
  end loop;
end;
您需要(
立即执行
):

declare
  d number;
  v_sql varchar2(1000);

  cursor c1 is select object_name 
    from all_objects 
    where object_type = 'TABLE' and owner = 'HR';

begin
  for r in c1 loop
      v_sql := 'select count(*) from HR.'||r.object_name;
      execute immediate v_sql into d;
      if d = 0 then
          dbms_output.put_line(r.object_name||' is empty');
      end if;
  end loop;
end;

在Oracle中,您可以使用以下查询

select    count(*) from    dba_tables  where    OWNER = 'XXXX' and num_rows =0;

在Oracle中,您可以使用以下查询

select    count(*) from    dba_tables  where    OWNER = 'XXXX' and num_rows =0;

查询
dba\u表.num\u行
是不可靠的,因为这是一个估计值。如果从未对该表进行过分析,则可能无法反映正确的行数

这也不需要动态SQL。基于此计算所有表的行数,您只需添加一个where条件:

select table_name
from (
  select table_name, 
         to_number(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) c from '||owner||'.'||table_name)),'/ROWSET/ROW/C')) as row_count
   from all_tables
   where owner = 'HR'
)
where row_count = 0;

查询
dba\u表.num\u行
是不可靠的,因为这是一个估计值。如果从未对该表进行过分析,则可能无法反映正确的行数

这也不需要动态SQL。基于此计算所有表的行数,您只需添加一个where条件:

select table_name
from (
  select table_name, 
         to_number(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) c from '||owner||'.'||table_name)),'/ROWSET/ROW/C')) as row_count
   from all_tables
   where owner = 'HR'
)
where row_count = 0;

它仍然抛出错误表或视图不存在添加所有者:
v_sql:=“从HR中选择count(*)。| | r.object_name仍然抛出错误的表或视图不存在添加所有者:
v_sql:=“从HR中选择count(*)。| | r.object_name您需要为任何人提供更多、具体的上下文,以帮助您搜索“动态SQL”的可能副本。您需要为任何人提供更多、具体的上下文,以帮助您搜索“动态SQL”的可能副本