用于在架构中查找表名称和计数(表名称)的sql查询
用于在架构中查找用于在架构中查找表名称和计数(表名称)的sql查询,sql,oracle,Sql,Oracle,用于在架构中查找表\u名称和计数(表\u名称)的sql查询: 例如: 我从该查询中获取表\u名称: SELECT * FROM USER_TABLES 和计数(表名称) 现在我想得到这样的结果: table_name count(table_name) Employee 100 dept 21 subjects 56 看起来像是一个简单的UNION ALL的典型用例: select 'Employee' table_name,
表\u名称
和计数(表\u名称)
的sql查询:
例如:
我从该查询中获取表\u名称:
SELECT * FROM USER_TABLES
和计数(表名称)
现在我想得到这样的结果:
table_name count(table_name)
Employee 100
dept 21
subjects 56
看起来像是一个简单的UNION ALL的典型用例:
select
'Employee' table_name,
count(*) from employee
union all
select
'dept' table_name,
count(*) from dept
union all
select
'subjects' table_name,
count(*) from subjects
如果要自动执行此操作,可以迭代用户表
:
declare
l_cnt pls_integer;
begin
for cur in (select table_name from user_tables order by table_name)
loop
execute immediate 'select count(*) from ' || cur.table_name into l_cnt;
dbms_output.put_line(cur.table_name || ' ' || l_cnt);
end loop;
end;
您还可以动态构建SQL语句并在以后使用它,而不是简单地打印结果:
declare
l_sql varchar2(4000);
begin
for cur in (select table_name from user_tables order by table_name)
loop
l_sql := l_sql || ' select ' || cur.table_name || ' as table_name, count(*) as cnt from ' || cur.table_name || ' union all';
end loop;
-- remove trailing UNION ALL
l_sql := regexp_replace(l_sql, 'union all$', '');
dbms_output.put_line(l_sql);
end;
看起来像是一个简单的UNION ALL的典型用例:
select
'Employee' table_name,
count(*) from employee
union all
select
'dept' table_name,
count(*) from dept
union all
select
'subjects' table_name,
count(*) from subjects
如果要自动执行此操作,可以迭代用户表
:
declare
l_cnt pls_integer;
begin
for cur in (select table_name from user_tables order by table_name)
loop
execute immediate 'select count(*) from ' || cur.table_name into l_cnt;
dbms_output.put_line(cur.table_name || ' ' || l_cnt);
end loop;
end;
您还可以动态构建SQL语句并在以后使用它,而不是简单地打印结果:
declare
l_sql varchar2(4000);
begin
for cur in (select table_name from user_tables order by table_name)
loop
l_sql := l_sql || ' select ' || cur.table_name || ' as table_name, count(*) as cnt from ' || cur.table_name || ' union all';
end loop;
-- remove trailing UNION ALL
l_sql := regexp_replace(l_sql, 'union all$', '');
dbms_output.put_line(l_sql);
end;
看起来像是一个简单的UNION ALL的典型用例:
select
'Employee' table_name,
count(*) from employee
union all
select
'dept' table_name,
count(*) from dept
union all
select
'subjects' table_name,
count(*) from subjects
如果要自动执行此操作,可以迭代用户表
:
declare
l_cnt pls_integer;
begin
for cur in (select table_name from user_tables order by table_name)
loop
execute immediate 'select count(*) from ' || cur.table_name into l_cnt;
dbms_output.put_line(cur.table_name || ' ' || l_cnt);
end loop;
end;
您还可以动态构建SQL语句并在以后使用它,而不是简单地打印结果:
declare
l_sql varchar2(4000);
begin
for cur in (select table_name from user_tables order by table_name)
loop
l_sql := l_sql || ' select ' || cur.table_name || ' as table_name, count(*) as cnt from ' || cur.table_name || ' union all';
end loop;
-- remove trailing UNION ALL
l_sql := regexp_replace(l_sql, 'union all$', '');
dbms_output.put_line(l_sql);
end;
看起来像是一个简单的UNION ALL的典型用例:
select
'Employee' table_name,
count(*) from employee
union all
select
'dept' table_name,
count(*) from dept
union all
select
'subjects' table_name,
count(*) from subjects
如果要自动执行此操作,可以迭代用户表
:
declare
l_cnt pls_integer;
begin
for cur in (select table_name from user_tables order by table_name)
loop
execute immediate 'select count(*) from ' || cur.table_name into l_cnt;
dbms_output.put_line(cur.table_name || ' ' || l_cnt);
end loop;
end;
您还可以动态构建SQL语句并在以后使用它,而不是简单地打印结果:
declare
l_sql varchar2(4000);
begin
for cur in (select table_name from user_tables order by table_name)
loop
l_sql := l_sql || ' select ' || cur.table_name || ' as table_name, count(*) as cnt from ' || cur.table_name || ' union all';
end loop;
-- remove trailing UNION ALL
l_sql := regexp_replace(l_sql, 'union all$', '');
dbms_output.put_line(l_sql);
end;
像这样试试
select
table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c ' ||
' from '||owner||'.'||table_name))
,'/ROWSET/ROW/C')) count
from all_tables
where table_name In (
SELECT table_name from user_tables )
或者使用Join
select
a.table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c ' ||
' from '||owner||'.'||a.table_name))
,'/ROWSET/ROW/C')) count
from all_tables a JOIN user_tables u ON
a.table_name=u.table_name AND a.owner = user
像这样试试
select
table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c ' ||
' from '||owner||'.'||table_name))
,'/ROWSET/ROW/C')) count
from all_tables
where table_name In (
SELECT table_name from user_tables )
或者使用Join
select
a.table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c ' ||
' from '||owner||'.'||a.table_name))
,'/ROWSET/ROW/C')) count
from all_tables a JOIN user_tables u ON
a.table_name=u.table_name AND a.owner = user
像这样试试
select
table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c ' ||
' from '||owner||'.'||table_name))
,'/ROWSET/ROW/C')) count
from all_tables
where table_name In (
SELECT table_name from user_tables )
或者使用Join
select
a.table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c ' ||
' from '||owner||'.'||a.table_name))
,'/ROWSET/ROW/C')) count
from all_tables a JOIN user_tables u ON
a.table_name=u.table_name AND a.owner = user
像这样试试
select
table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c ' ||
' from '||owner||'.'||table_name))
,'/ROWSET/ROW/C')) count
from all_tables
where table_name In (
SELECT table_name from user_tables )
或者使用Join
select
a.table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c ' ||
' from '||owner||'.'||a.table_name))
,'/ROWSET/ROW/C')) count
from all_tables a JOIN user_tables u ON
a.table_name=u.table_name AND a.owner = user
这就是我的想法。现在我认为OP不想硬编码表名,所以应该使用一些动态Sql。@frank:该模式有200个表,我不能为所有表编写union,所以我想用一种简单的方法来实现。这就是我的想法。现在我认为OP不想硬编码表名,所以应该使用一些动态Sql。@frank:该模式有200个表,我不能为所有表编写union,所以我想用一种简单的方法来实现。这就是我的想法。现在我认为OP不想硬编码表名,所以应该使用一些动态Sql。@frank:该模式有200个表,我不能为所有表编写union,所以我想用一种简单的方法来实现。这就是我的想法。现在我认为OP不想硬编码表名,所以应该使用一些动态Sql。@frank:该模式有200个表,我不能为所有表编写union,所以我想用一种简单的方法来实现这一点。+1很好。但是,我建议使用联接,而不是在中使用
作为表的名称,否则,如果在不同的模式中存在同名的表,您可能会得到额外的行。@FrankSchmitt先生。更新答案。第二个版本仍然存在相同的问题。您需要在连接条件中添加`AND a.owner=user',或者您可以简单地使用user\u表而不是所有的\u表。+1很好。但是,我建议使用联接,而不是在
中使用作为表的名称,否则,如果在不同的模式中存在同名的表,您可能会得到额外的行。@FrankSchmitt先生。更新答案。第二个版本仍然存在相同的问题。您需要在连接条件中添加`AND a.owner=user',或者您可以简单地使用user\u表而不是所有的\u表。+1很好。但是,我建议使用联接,而不是在
中使用作为表的名称,否则,如果在不同的模式中存在同名的表,您可能会得到额外的行。@FrankSchmitt先生。更新答案。第二个版本仍然存在相同的问题。您需要在连接条件中添加`AND a.owner=user',或者您可以简单地使用user\u表而不是所有的\u表。+1很好。但是,我建议使用联接,而不是在
中使用作为表的名称,否则,如果在不同的模式中存在同名的表,您可能会得到额外的行。@FrankSchmitt先生。更新答案。第二个版本仍然存在相同的问题。您需要将“AND a.owner=user”添加到加入条件中,或者您可以简单地使用user_表而不是所有_表。为什么不接受问题中的任何答案?为什么不接受问题中的任何答案?为什么不接受问题中的任何答案?为什么不接受问题中的任何答案?