脚本SQL,用于汇总来自不同DB的表的值

时间:2016-08-01 14:59:08

标签: sql database oracle plsql

我需要一个脚本,它使来自不同数据库的space_limit的{​​{1}}之和: 我有一个5个DB的列表,我需要它连接到每个DB,将该值(space_limit)保存在内存中,最后给出所有5个值的总和

有可能吗?

这是我到目前为止所得到的,在底部我需要它来显示总和:

v$recovery_file_dest

/ .....等等每个数据库5次..... /

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/&pswuser@DB1
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
/

更新: 我尝试为每个数据库添加此内容

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

spool off
set markup html off
disc

但它给了我这个错误

第2行的

错误: ORA-06550:第2行,第32栏: PLS-00357:在此上下文中不允许使用表,视图或序列引用'V $ RECOVERY_FILE_DEST.SPACE_LIMIT' ORA-06550:第2行第2列: PL / SQL:忽略语句

1 个答案:

答案 0 :(得分:1)

您尝试保持运行总数并不是那么遥远,但您需要在SQL上下文中组合绑定变量和表值:

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

然后,您可以print总计,或者保持HTML格式从dual查询。因此,您的脚本可能最终看起来像:

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/&pswuser@DB1
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>
&#13;
&#13;
&#13;

相关问题