我正在尝试执行以下查询并且失败。如何修复它?
decalre curPar int;
begin
select degree int curPar from dba_indexes where index_name='Z';
alter index "SAPR3"."Z" rebuild online parallel 3;
alter index "SAPR3"."Z" parallel curPar;
select degree from dba_indexes where index_name='Z';
end;
--- Solution is not working
var curPar number;
exec select degree into :max_degree from dba_indexes where index_name='Z';
alter index "SAPR3"."Z" rebuild online parallel 3;
alter index "SAPR3"."Z" parallel curPar;
select degree from dba_indexes where index_name='Z';
/
答案 0 :(得分:3)
声明而不是decalre
SELECT ... INTO而不是SELECT ... INT
数据类型int - 在Oracle中不存在
只能使用动态SQL(立即执行)
来使用DDL操作(alter index)declare
curPar NUMBER;
begin
select degree into curPar from dba_indexes where index_name='Z';
execute immediate 'alter index "SAPR3"."Z" rebuild online parallel 3';
execute immediate 'alter index "SAPR3"."Z" parallel '||curPar;
select degree into curPar from dba_indexes where index_name='Z';
end;
答案 1 :(得分:1)
alter index不是PLSQL命令。使用EXECUTE IMMEDIATE
declare curPar integer;
begin
select degree into curPar from dba_indexes where index_name='Z';
execute immediate 'alter index "SAPR3"."Z" rebuild online parallel 3';
execute immediate 'alter index "SAPR3"."Z" parallel curPar';
select degree into curPar from dba_indexes where index_name='Z';
end;
答案 2 :(得分:0)
您无法在pl / sql块中执行DDL查询。如果您需要这样做,可以使用EXECUTE IMMEDIATE
。
SQL> begin
2 alter index i1 rebuild parallel 1;
3 end;
4 /
alter index i1 rebuild parallel 1;
*
ERROR at line 2:
ORA-06550: line 2, column 5:
PLS-00103: Encountered the symbol "ALTER" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
使用execute immediate
:
SQL> declare
2 vSQL varchar2(100);
3 curPar integer;
4 begin
5 select 1 into curPar from dual;
6 vSQL := 'alter index i1 rebuild parallel ' || curPar;
7 execute immediate vSQL;
8 end;
9 /
PL/SQL procedure successfully completed.