--存储过程已经成功建立,但调用出错,望指点
create or replace Procedure Pcreatetab(b out boolean)   --动态建立表TEMP
as 
dsql varchar2(4000);--删除表用的
csql varchar2(4000);--创建表用的
temp_fldcnt number;--TEMP现列数
at_fldcnt number;--ALARMTYP列数
cnt number;
i number;
begin
     b:=false;
     --判断是否存在表TEMP
     select count(*) into cnt from user_tables where table_name='TEMP';
     if cnt>0 then
        --取得字段数
        select count(*) into temp_fldcnt from user_col_comments where table_name='TEMP';
        select count(*) into at_fldcnt from user_col_comments where table_name='ALARMTYPE';
        if temp_fldcnt<>at_fldcnt then
           --删除TEMP后再建立
           dsql:='DROP TABLE TEMP;';
           --以ALARMTYPE字段数为准建立临时表TEMP
           csql:='CREATE GROBAL TEMPORARY TEMP(';
           i:=1;
           while i<=at_fldcnt loop
              csql:=csql || 'f' || to_char(i) || ' number,'
              i:=i+1;
           end loop;
           --去掉多余的,号
           csql:=substr(csql,1,length(csql)-1);
           csql:=csql || ');'
           --执行
           execute immediate dsql;
           execute immediate csql;
        end if;
    else--不存在表TEMP
       select count(*) into at_fldcnt from user_col_comments where table_name='ALARMTYPE';
       csql:='CREATE GROBAL TEMPORARY TEMP(';
       i:=1;
       while i<=at_fldcnt loop
           csql:=csql || 'f' || to_char(i) || ' number,'
           i:=i+1;
       end loop;
       --去掉多余的,号
       csql:=substr(csql,1,length(csql)-1);
       csql:=csql || ');'
       --执行
       execute immediate csql;
    end if;
    b:=true;
end;--测试这部分的时候就通不过
else--不存在表TEMP
select count(*) into at_fldcnt from user_col_comments where table_name='ALARMTYPE';
csql:='CREATE GROBAL TEMPORARY TEMP(';
i:=1;
while i<=at_fldcnt loop
csql:=csql || 'f' || to_char(i) || ' number,'
i:=i+1;
end loop;
--去掉多余的,号
csql:=substr(csql,1,length(csql)-1);
csql:=csql || ');'
--执行
execute immediate csql;
end if;

解决方案 »

  1.   

    csql:=csql || ');'这里错了
      

  2.   

    create or replace Procedure Pcreatetab(b out boolean)   --动态建立表TEMP
    as 
    dsql varchar2(4000);--删除表用的
    csql varchar2(4000);--创建表用的
    temp_fldcnt number;--TEMP现列数
    at_fldcnt number;--ALARMTYP列数
    cnt number;
    i number;
    begin
         b:=false;
         --判断是否存在表TEMP
         select count(*) into cnt from user_tables where table_name='TEMP';
         if cnt>0 then
            --取得字段数
            select count(*) into temp_fldcnt from user_col_comments where table_name='TEMP';
            select count(*) into at_fldcnt from user_col_comments where table_name='ALARMTYPE';
            if temp_fldcnt<>at_fldcnt then
               --删除TEMP后再建立
               dsql:='DROP TABLE TEMP;';
               --以ALARMTYPE字段数为准建立临时表TEMP
               csql:='CREATE GROBAL TEMPORARY TABLE TEMP(';
               i:=1;
               while i<=at_fldcnt loop
                  csql:=csql || 'f' || to_char(i) || ' number,'
                  i:=i+1;
               end loop;
               --去掉多余的,号
               csql:=substr(csql,1,length(csql)-1);
               csql:=csql || ');'
               --执行
               execute immediate dsql;
               execute immediate csql;
            end if;
        else--不存在表TEMP
           select count(*) into at_fldcnt from user_col_comments where table_name='ALARMTYPE';
           csql:='CREATE GROBAL TEMPORARY TABLE TEMP(';
           i:=1;
           while i<=at_fldcnt loop
               csql:=csql || 'f' || to_char(i) || ' number,'
               i:=i+1;
           end loop;
           --去掉多余的,号
           csql:=substr(csql,1,length(csql)-1);
           csql:=csql || ')';
           --执行
           execute immediate csql;
        end if;
        b:=true;
    end;