2016-08-01 52 views
0

我需要一個腳本,使與v$recovery_file_dest之和來自不同的數據庫: 我有一個5個DB的列表,我需要它連接到每個數據庫,保存該值(space_limit)在內存中,並在最後給我所有5個值的總和腳本SQL從不同數據庫中總結表的值

這可能嗎?

這是我走到這一步,在底部,我需要它顯示的總和:

undefine user 
accept user char prompt 'User : ' 
undefine pswuser 
accept pswuser char prompt 'Password : ' HIDE 

set trimout off 
set verify off 
set markup html on 

spool Z:\....\...\FRA_report_&data._&ora..html 

Prompt ##################################################### 
Prompt DATABASE 1 
Prompt ##################################################### 
connect &user/&[email protected] 
select name, 
round(space_limit/1024/ 1024), 
to_char(round(space_used/1048576),'999g999g990','NLS_NUMERIC_CHARACTERS=,.'), 
round(((space_used/1048576)/(space_limit/1048576)*100),2)||'%' 
from v$recovery_file_dest 
/

/.....And等5次,每次DB ...../

Prompt ##################################################### 
Prompt TOTAL FRA 
Prompt ##################################################### 

spool off 
set markup html off 
disc 

更新: 我嘗試添加該每個DB

begin 
:total := total + v$recovery_file_dest.space_limit; 
end; 
/

但它給了我這個錯誤

ERROR位於第2行: ORA-06550:2號線,列32: PLS-00357:表,視圖或序列參考 'V $ RECOVERY_FILE_DEST.SPACE_LIMIT' 在這種情況下不允許的 ORA-06550:第2行2列: PL/SQL:語句被忽略

+1

連接到每一個從那裏 - 單個客戶端會話內?從shell /批處理腳本?是否有數據庫鏈接從你的一個數據庫到其他數據庫? –

+0

我從SQLPlus連接,並不是所有的數據庫都鏈接到對方! – gmaster

+0

您不清楚在腳本中哪些地方添加了更新的行,請對其進行編輯,以便我們可以在腳本中看到它的位置。 –

回答

1

你也不會太遠了與你的企圖保持運行總和,但你需要綁定變量與表結合值在SQL上下文中:

begin 
    select nvl(:total,0) + sum(space_limit) into :total from v$recovery_file_dest; 
end; 
/

然後您可以從總數print中選擇,或者從dual開始保留HTML格式的查詢。所以,你的腳本最終可能會看起來像:

variable total number; 

undefine user 
accept user char prompt 'User : ' 
undefine pswuser 
accept pswuser char prompt 'Password : ' HIDE 

set trimout off 
set verify off 
set markup html on 
set numformat 999999999999 

spool Z:\....\...\FRA_report_&data._&ora..html 

Prompt ##################################################### 
Prompt DATABASE 1 
Prompt ##################################################### 
connect &user/&[email protected] 
set feedback off 

select name, 
round(space_limit/1024/ 1024), 
to_char(round(space_used/1048576),'999g999g990','NLS_NUMERIC_CHARACTERS=,.'), 
round(((space_used/1048576)/(space_limit/1048576)*100),2)||'%' 
from v$recovery_file_dest 
/

exec select nvl(:total,0) + sum(space_limit) into :total from v$recovery_file_dest; 

-- repeat for other databases 

Prompt ##################################################### 
Prompt TOTAL FRA 
Prompt ##################################################### 

select :total as total_fra, :total/(1024*1024) as total_fra_mb from dual; 

spool off 
set markup html off 
disc 

我還添加變量聲明,並設置數字格式,因此不會進入科學記數法爲大值。當然,您可以操縱:total值以MB或GB或任何您喜歡的方式顯示它 - 我已經顯示了原始值和MB值以匹配各個數據庫值。

我也添加了set feedback off,必須在每個connect之後重複 - 某些設置在您重新連接時會重置。

即代碼產生這樣的輸出,當與兩個數據庫運行列出:

##################################################### 
 
<br> 
 
DATABASE 1 
 
<br> 
 
##################################################### 
 
<br> 
 
<p> 
 
<table border='1' width='90%' align='center' summary='Script output'> 
 
<tr> 
 
<th scope="col"> 
 
NAME 
 
</th> 
 
<th scope="col"> 
 
ROUND(SPACE_LIMIT/1024/1024) 
 
</th> 
 
<th scope="col"> 
 
TO_CHAR(ROUN 
 
</th> 
 
<th scope="col"> 
 
ROUND(((SPACE_USED/1048576)/(SPACE_LIMIT/ 
 
</th> 
 
</tr> 
 
<tr> 
 
<td> 
 
+FRA 
 
</td> 
 
<td align="right"> 
 
     30720 
 
</td> 
 
<td> 
 
     24.570 
 
</td> 
 
<td> 
 
79.98% 
 
</td> 
 
</tr> 
 
</table> 
 
<p> 
 
##################################################### 
 
<br> 
 
DATABASE 2 
 
<br> 
 
##################################################### 
 
<br> 
 
<p> 
 
<table border='1' width='90%' align='center' summary='Script output'> 
 
<tr> 
 
<th scope="col"> 
 
NAME 
 
</th> 
 
<th scope="col"> 
 
ROUND(SPACE_LIMIT/1024/1024) 
 
</th> 
 
<th scope="col"> 
 
TO_CHAR(ROUN 
 
</th> 
 
<th scope="col"> 
 
ROUND(((SPACE_USED/1048576)/(SPACE_LIMIT/ 
 
</th> 
 
</tr> 
 
<tr> 
 
<td> 
 
+FRA 
 
</td> 
 
<td align="right"> 
 
     24576 
 
</td> 
 
<td> 
 
     12.698 
 
</td> 
 
<td> 
 
51.67% 
 
</td> 
 
</tr> 
 
</table> 
 
<p> 
 
##################################################### 
 
<br> 
 
TOTAL FRA 
 
<br> 
 
##################################################### 
 
<br> 
 
<p> 
 
<table border='1' width='90%' align='center' summary='Script output'> 
 
<tr> 
 
<th scope="col"> 
 
TOTAL_FRA 
 
</th> 
 
<th scope="col"> 
 
TOTAL_FRA_MB 
 
</th> 
 
</tr> 
 
<tr> 
 
<td align="right"> 
 
    57982058496 
 
</td> 
 
<td align="right"> 
 
     55296 
 
</td> 
 
</tr> 
 
</table> 
 
<p>

+0

完美,正是我需要的感謝! – gmaster

相關問題