2009-06-03 57 views
1

我有這個服務器有以下系統:如何許多SQL查詢結果集成到一個輸出(甲骨文)

的Windows 2003 R2耳鼻喉科版SP 2 甲骨文1.0.2.0.4.0 數據庫:5

這些數據庫的歸檔日誌位於閃回恢復區,如v $ recovery_file_dest的NAME列所示,還可以通過調用sqlplus中的「show parameter db_recovery_file_dest」來進行。我想用SQL或PLSQL腳本來監視每個數據庫的歸檔日誌的空間使用情況。

如果我引用下面的查詢:

SET LINESIZE 145 
SET PAGESIZE 9999 

COLUMN name    FORMAT a30     HEADING 'Name' 
COLUMN space_limit  FORMAT 99,999,999,999,999 HEADING 'Space Limit' 
COLUMN space_used   FORMAT 99,999,999,999,999 HEADING 'Space Used' 
COLUMN space_used_pct  FORMAT 999.99    HEADING '% Used' 
COLUMN space_reclaimable FORMAT 99,999,999,999,999 HEADING 'Space Reclaimable' 
COLUMN pct_reclaimable FORMAT 999.99    HEADING '% Reclaimable' 
COLUMN number_of_files FORMAT 999,999    HEADING 'Number of Files' 


prompt 
prompt Current location, disk quota, space in use, space reclaimable by deleting files, 
prompt and number of files in the Flash Recovery Area. 
prompt 

SELECT 
    name 
    , space_limit 
    , space_used 
    , ROUND((space_used/space_limit)*100, 2) space_used_pct 
    , space_reclaimable 
    , ROUND((space_reclaimable/space_limit)*100, 2) pct_reclaimable 
    , number_of_files 
FROM 
    v$recovery_file_dest 
ORDER BY 
    name 
/

我會得到這樣的輸出:

Name         Space Limit   Space Used % Used Space Reclaimable % Reclaimable Number of Files     
------------------------------ ------------------- ------------------- ------- ------------------- ------------- ---------------     
D:\oracle\product\10.2.0\flash  107,374,182,400  34,239,603,712 31.89     0   .00    804     
_recovery_area\DBNAME 

我的問題是:我怎麼每個類似的查詢結果整合來自每個數據庫成一個輸出?

輸出應該是這樣的:

Name         Space Limit   Space Used % Used Space Reclaimable % Reclaimable Number of Files     
    ------------------------------ ------------------- ------------------- ------- ------------------- ------------- ---------------     
    D:\oracle\product\10.2.0\flash  107,374,182,400  34,239,603,712 31.89     0   .00    804     
_recovery_area\DBNAME1 

D:\oracle\product\10.2.0\flash  107,374,182,400  34,239,603,712 31.89     0   .00    804     
    _recovery_area\DBNAME2 

D:\oracle\product\10.2.0\flash  107,374,182,400  34,239,603,712 31.89     0   .00    804     
    _recovery_area\DBNAME3 

回答

2

使用UNION加入單獨的查詢結果爲一組。例如:

select name, space_limit, etc... 
from v$recovery_file_dest 
UNION 
select name, space_limit, etc... 
from v$recovery_file_dest -- presumably a different value than the first one 

應該沒問題,因爲每個查詢以相同順序返回相同的一組列。

+3

這涉及到SQL Server,而不是Oracle,所以它可能不一樣 - 但它可能是:如果你知道你的行已經不同了,那就使用UNION ALL。簡單的UNION要求DBMS花費額外的時間去除出現重複行 – RolandTumble 2009-06-03 23:04:55

3

我假設你想要在一個結果集中的所有數據庫結果,而不是隻依次連接到每個結果集,並將輸出後臺處理爲同一個文件。你必須挑選其中一個數據庫和創建數據庫鏈接到其他四個(或選擇在每個有V $ RECOVERY_FILE_DEST SELECT權限創建連接的用戶。

然後將查詢變得

select ... 
from v$recover_file_dest 
union all 
select ... 
from [email protected] 
union all 
select ... 
from [email protected] 
union all 
select ... 
from [email protected]