[英]SQL Server Select from a table returned by a query as list of table names
[英]How to perform a select from table names returned by another query?
我有一個查詢返回我需要的表名,例如
select table_name from all_tables where lower(table_name) like 'test_table%';
如何根據查詢結果從其收到的所有表中執行選擇? 我嘗試連接它們並使用“立即執行”,但我無法使它工作。
我試過了:
execute immediate 'select value from '|| select listagg(table_name, ', ') within group (order by table_name) from all_tables where table_name like 'test_table%';
下面是一個在查詢返回的表列表上執行“select count(*)”的小例子:
select
TRUNC(SYSDATE) AS COUNT_DATE,
table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c from '||owner||'.'||table_name))
,'/ROWSET/ROW/C')) as count
from
all_tables where rownum < 3
order by
table_name
dbms_xmlgen.getxml允許您執行動態創建的查詢並將結果作為XML返回,您可以使用extractvalue從中提取值。 當您要發出的查詢返回單行時有用。
問候
大號
你可以這樣做:
select 'select * from '||owner||'.'||table_name||';'
from all_tables
where lower(table_name) like '%test_table%';
然后復制並粘貼結果並以腳本或根據需要單獨運行語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.