一个存储过程游标使用的错误,请帮忙看看。
create or replace procedure sp_DoHouseKeeping
as
strSQLvarchar2(2048);
begin
-- 声明一个游标
cursor mycursor is
select
'alter database datafile '''||a.file_name||''' resize '
||round(a.filesize - (a.filesize - c.hwmsize-100) *0.8)||'M;' runstatement
from
(
select file_id,file_name,round(bytes/1024/1024) filesize from dba_data_files
) a,
(
select file_id,round(max(block_id)*8/1024) HWMsize from dba_extents
group by file_id
) c
where a.file_id = c.file_id
and a.filesize - c.hwmsize > 100;
-- 声明一个游标变量
rowdata mycursor%rowtype;
begin
-- 处理数据
for rowdata in mycursor loop
-- 构造PL/SQL语句
strSQL:=rowdata.runstatement;
-- 打印SQL语句
DBMS_OUTPUT.PUT_LINE(strSQL);
-- 执行动态SQL语句
execute immediate strSQL;
commit;
end loop;
end;
end;
SQL> create or replace procedure sp_DoHouseKeeping
2 as
3 cursor cur1 is
4 select * from dba_data_files;
5 begin
6 null;
7 end;
8 /
警告: 创建的过程带有编译错误。
SQL>
SQL>
SQL> create or replace procedure sp_DoHouseKeeping
2 as
3 cursor cur1 is
4 select * from emp;
5 begin
6 null;
7 end;
8 /
过程已创建。
create or replace procedure sp_DoHouseKeeping
as
strSQL varchar2(2048);
cursor mycursor is
select
'alter database datafile '''
[解决办法]
a.file_name
[解决办法]
''' resize '
[解决办法]
round(a.filesize - (a.filesize - c.hwmsize-100) *0.8)
[解决办法]
'M' runstatement
from
(
select file_id,file_name,round(bytes/1024/1024) filesize from dba_data_files
) a,
(
select file_id,round(max(block_id)*8/1024) HWMsize from dba_extents
group by file_id
) c
where a.file_id = c.file_id
and a.filesize - c.hwmsize > 100;
begin
open mycursor;
fetch mycursor into strSQL;
loop
exit when mycursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(strSQL);
execute immediate strSQL;
commit;
fetch mycursor into strSQL;
end loop;
close mycursor;
null;
end;
/