重命名分区错误的功能

时间:2014-12-12 16:53:58

标签: sql oracle function plsql

我正在尝试使用以下功能重命名分区表的分区:

create or replace 
function         MYFUNCTION(PSCHEMANAME in varchar2, PTABLENAME in varchar2,PINITNAME in varchar2) 
  return varchar2 
  is 

 DEFAULT_NAME varchar2(10);
 LRETVALUE VARCHAR2(1000):='0';
 EMESG varchar2(1000); 
 ECODE    NUMBER(38);

BEGIN
 FOR X IN (SELECT PARTITION_NAME, HIGH_VALUE FROM USER_TAB_PARTITIONS WHERE TABLE_NAME = 'PTABLENAME' AND PARTITION_NAME NOT LIKE 'PINITNAME_%')
 LOOP
   execute immediate 'select to_char('||X.HIGH_VALUE||'-1,''YYYYMMDD'') from dual' into DEFAULT_NAME;
   execute immediate 'alter table'|| PTABLENAME ||' rename partition '||X.PARTITION_NAME||' to PINITNAME_'||DEFAULT_NAME;
   LRETVALUE:= ('0-Successful');
        RETURN lretvalue;
 END LOOP;
  EXCEPTION
       when OTHERS then lretvalue := '1,Problem....'||sqlcode||','||sqlerrm||';
        RETURN lretvalue;
END MYFUNCTION;

我收到一条错误说...已完成对PL / SQL函数的调用,但没有执行RETURN语句..

1 个答案:

答案 0 :(得分:0)

将RETURN放出循环:

create or replace 
function         MYFUNCTION(PSCHEMANAME in varchar2, PTABLENAME in varchar2,PINITNAME in varchar2) 
  return varchar2 
  is 

 DEFAULT_NAME varchar2(10);
 LRETVALUE VARCHAR2(256) := '0';
 EMESG varchar2(1000); 
 ECODE    NUMBER(38);

BEGIN
 FOR X IN (SELECT PARTITION_NAME, HIGH_VALUE FROM USER_TAB_PARTITIONS WHERE TABLE_NAME = PTABLENAME AND PARTITION_NAME NOT LIKE 'PINITNAME_%')
 LOOP
   execute immediate 'select to_char('||X.HIGH_VALUE||'-1,''YYYYMMDD'') from dual' into DEFAULT_NAME;
   execute immediate 'alter table '|| PTABLENAME ||' rename partition '||X.PARTITION_NAME||' to PINITNAME_'||DEFAULT_NAME;
 END LOOP;

 LRETVALUE := ('0-Successful');
 RETURN LRETVALUE;

  EXCEPTION
       when OTHERS then RETURN '1,Problem....'||sqlcode||','||sqlerrm;        
END MYFUNCTION;
/