Oracle 动态查找每个 table 的行数和列数

Oracle dynamic find number of rows and columns per table

我有以下查询,其中 returns 模式中每个 table 的行数。

这也可以修改为 RETURN 每 table 的列数吗?


CREATE table stats (
table_name VARCHAR2(128),
num_rows NUMBER,
num_cols NUMBER 
);
/

DECLARE
val integer;
BEGIN
for i in (SELECT table_name FROM all_tables WHERE owner = 'Schema')
LOOP
EXECUTE IMMEDIATE 'SELECT count(*) from ' || i.table_name INTO val;
INSERT INTO stats VALUES (i.table_name,val);
END LOOP;
END;
/

您可以使用 ALL_TAB_COLS 字典 table:

DECLARE
  val integer;
BEGIN
  FOR i IN (
    SELECT table_name,
           COUNT(*) AS num_cols
    FROM   all_tab_cols
    WHERE  owner = 'Schema'
    GROUP BY table_name
  )
  LOOP
    EXECUTE IMMEDIATE 'SELECT count(*) from ' || i.table_name INTO val;
    INSERT INTO stats VALUES (i.table_name,val, i.num_cols);
  END LOOP;
END;
/

db<>fiddle here