2016-08-01 47 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

相关问题