通过SQL*Plus在Oracle数据库中的几个表显示计数

发布于 2025-02-03 17:55:43 字数 566 浏览 1 评论 0原文

我正在尝试在Oracle数据库中显示所有类似名称的表的计数。查询将通过命令行中的SQL*Plus运行。我已经编写了以下代码:

DECLARE
   ctr number;
   cursor to_check is
     select table_name from user_tables
       where table_name like '%REF%' or table_name like '%CNF%'
       order by table_name;
    
 BEGIN
 set serveroutput on size 100000;
   for rec in to_check loop
     execute immediate 'select count(*) into :ctr from ' || rec.table_name;
     dbms_output.put_line(rec.table_name || '     ' || ctr);
   end loop;
 END;

但是,我没有收到此代码的输出。 DBMS_OUTPUT是否有问题?还是我错过了一种更优雅的拉动和显示此信息的方式?

I'm attempting to display the count of all tables of a similar name in an Oracle database. The query will run via SQL*Plus at the command line. I've written the following code:

DECLARE
   ctr number;
   cursor to_check is
     select table_name from user_tables
       where table_name like '%REF%' or table_name like '%CNF%'
       order by table_name;
    
 BEGIN
 set serveroutput on size 100000;
   for rec in to_check loop
     execute immediate 'select count(*) into :ctr from ' || rec.table_name;
     dbms_output.put_line(rec.table_name || '     ' || ctr);
   end loop;
 END;

However, I'm not receiving the output of this code. Is there an issue with the dbms_output? Or am I missing a more elegant way of pulling and displaying this information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

债姬 2025-02-10 17:55:43

我是否错过了一种更优雅的拉动和显示此信息的方式?

这有点主观,但是...

您可以避免使用XML技巧避免使用PL/SQL(并需要客户端显示输出)。您可以使用dbms_xmlgen生成包含所需信息的XML文档:

select dbms_xmlgen.getxmltype(
  'select ''' || table_name || ''' as table_name, count(*) as row_count'
    || ' from "' || table_name || '"')
from user_tab_columns
where table_name like '%REF%' or table_name like '%CNF%';

提取值

select x.table_name, x.row_count
from (
  select dbms_xmlgen.getxmltype(
    'select ''' || table_name || ''' as table_name, count(*) as row_count'
      || ' from "' || table_name || '"') as xml
  from user_tab_columns
  where table_name like '%REF%' or table_name like '%CNF%'
) t
cross apply xmltable(
  '/ROWSET/ROW'
  passing t.xml
  columns table_name varchar2(30) path 'TABLE_NAME',
    row_count number path 'ROW_COUNT'
) x
order by x.table_name;

然后将其用作CTE或INLINE视图,然后使用XMLTable: db<

am I missing a more elegant way of pulling and displaying this information?

That's a bit subjective, but...

You can avoid PL/SQL (and needing the client to display the output) with an XML trick. You can use dbms_xmlgen to generate an XML document containing the information you want:

select dbms_xmlgen.getxmltype(
  'select ''' || table_name || ''' as table_name, count(*) as row_count'
    || ' from "' || table_name || '"')
from user_tab_columns
where table_name like '%REF%' or table_name like '%CNF%';

And then use that as a CTE or inline view and extract the values using XMLTable:

select x.table_name, x.row_count
from (
  select dbms_xmlgen.getxmltype(
    'select ''' || table_name || ''' as table_name, count(*) as row_count'
      || ' from "' || table_name || '"') as xml
  from user_tab_columns
  where table_name like '%REF%' or table_name like '%CNF%'
) t
cross apply xmltable(
  '/ROWSET/ROW'
  passing t.xml
  columns table_name varchar2(30) path 'TABLE_NAME',
    row_count number path 'ROW_COUNT'
) x
order by x.table_name;

db<>fiddle with a couple of dummy tables.

过期以后 2025-02-10 17:55:43

上的ServerOutput设置是sql*plus命令。它不能出现在PL/SQL块中。它必须是一个单独的命令(并且仅在sql*plus客户端或实现sql*sql​​plus's set set set 的客户端或客户端中有意义/代码>命令)。在您的PL/SQL块执行之前,需要完成。

的语法即时执行将是

execute immediate 'select count(*) from ' || rec.table_name
   into ctr;

set serveroutput on is a SQL*Plus command. It cannot appear inside a PL/SQL block. it has to be a separate command (and is only meaningful in the SQL*Plus client or a client that implements a subset of SQL*Plus's set commands). That would need to be done before your PL/SQL block executes.

The syntax for execute immediate would be

execute immediate 'select count(*) from ' || rec.table_name
   into ctr;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文