1.首先是自动扩展表空间sql
首先查看表空间情况
select tablespace_name,file_name,autoextensible from dba_data_files where tablespace_name = '表空间名称';
如果这块没设置的话是NO
然后执行(第一个为例)
alter database datafile '/data/oracle/oradata/qmsdb/agqms001.dbf' autoextend on;
最后验证结果
select tablespace_name,file_name,autoextensible from dba_data_files where tablespace_name = '表空间名称';
开启语法
开启自动扩展功能语法:
alter database datafile '对应的数据文件路径信息' autoextend on;
关闭自动扩展功能语法:
alter database datafile '对应的数据文件路径信息' autoextend off;
2.自动新增表空间文件存储过程
CREATE OR REPLACE PROCEDURE auto_datafiles AUTHID CURRENT_USER is
t_sql varchar2(500);
t_datafile varchar2(500);
--创建游标
cursor tbspace_data is
--查询表空间使用情况
SELECT d.tablespace_name Name,
d.status "Status",
TO_CHAR(NVL(a.BYTES / 1024 / 1024 / 1024, 0), '99,999,990.90') "Size (G)",
TO_CHAR(NVL(a.BYTES - NVL(f.BYTES, 0), 0) / 1024 / 1024 / 1024,
'99999999.99') "USE",
TO_CHAR(NVL((a.BYTES - NVL(f.BYTES, 0)) / a.BYTES * 100, 0),
'990.00') Used
FROM SYS.dba_tablespaces d,
(SELECT tablespace_name, SUM(BYTES) BYTES
FROM dba_data_files
GROUP BY tablespace_name) a,
(SELECT tablespace_name, SUM(BYTES) BYTES
FROM SYS.dba_free_space
GROUP BY tablespace_name) f
WHERE d.tablespace_name = a.tablespace_name(+)
AND d.tablespace_name = f.tablespace_name(+)
AND NOT (d.extent_management LIKE 'LOCAL' AND
d.CONTENTS LIKE 'TEMPORARY');
begin
--遍历查询中的字段
for tbspace_all in tbspace_data loop
--增加系统表空间文件
If tbspace_all.Used >= 90 then
if tbspace_all.Name = 'USERS' OR tbspace_all.Name = 'SYSAUX' OR
tbspace_all.Name = 'SYSTEM' OR tbspace_all.Name = 'AGQMS' then
t_datafile := tbspace_all.Name;
t_datafile := '/upspace1/' || t_datafile || '_' ||
to_char(sysdate, 'yyyymmdd') || '.dbf';
t_sql := 'alter tablespace ' || tbspace_all.Name ||
' add datafile ''' || t_datafile || ''' size 2G';
dbms_output.put_line(t_sql);
Execute Immediate t_sql;
commit;
--增加自定义表空间文件
t_datafile := 'agqms';
t_datafile := t_datafile || tbspace_all.Name || '_' ||
to_char(sysdate, 'yyyymmdd') || '.dbf';
t_sql := 'alter tablespace ' || tbspace_all.Name ||
' add datafile ''' || t_datafile || ''' size 31G';
Execute Immediate t_sql;
commit;
end if;
end if;
end loop;
end auto_datafiles;
说明
1.开头要加AUTHID CURRENT_USER is声明调用者权限,要不然默认是定义者
2.要用sys用户登录给qms用户授权dba_tablespaces ,dba_data_files,dba_free_space 三个视图,要不然会提示表或者视图不存在,授权语句如下
grant select on dba_free_space to qms
3.该存储过程需要在qms用户下执行