以下是存储过程--开始
create or replace  procedure ZHUYE    as 
beginbegin                                --如果表存在则删除
                     
execute immediate ' drop table zhuyecompany';
exception when others then
null;
end;
create table   zhuyecompany(             ---创建表pk_costsubj varchar(50),
costcode    varchar(50),
costname    varchar(50),
zyjg        float,                      --主业机关
yisc         float,   
hj        float
)
                                                 create table sum_costsubj as
select    b.settleunitname,f.pk_settleunit,fb.pk_costsubj,sum(plan_money) as plan_money 
from fp_planbill f left join bd_settleunit b   on f.pk_settleunit=b.pk_settleunit
                   left join fp_planbill_b fb  on f.pk_planbill=fb.pk_planbill
                   where 1 = 1 and f.pk_corp = '1051' and f.vbillstatus >= '3' 
group by b.settleunitname,f.pk_settleunit,fb.pk_costsubj  
insert into zhuyecompany(pk_costsubj,costcode,costname)
select pk_costsubj,costcode,costname from bd_costsubj where pk_corp='1051' and costcode not in('5','9','901','902') order by costcodeupdate zhuyecompany set zyjg=(select plan_money from sum_costsubj where zhuyecompany.pk_costsubj=sum_costsubj.pk_costsubj and sum_costsubj.settleunitname='石家庄东方龙供水有限责任公司')update zhuyecompany set yisc=(select plan_money from sum_costsubj where zhuyecompany.pk_costsubj=sum_costsubj.pk_costsubj and sum_costsubj.settleunitname='石家庄东方龙供水有限责任公司一水厂')update zhuyecompany set hj=zyjg+yisc+ersc+sansc+sisc+wusc+liusc+qisc+basc+jxfugs+gxglc+wxgs+jlglzx+jyysbgs+zhjds+yhfwzx+szjcz+gsjls+hqglzx+jsjc+zgyy+ymyey where 1=1end;
EXECCUTE  ZHUYE

解决方案 »

  1.   

    1.create table 语句也应该用 execute immediate来执行。
    2.下面的insert,update语句最后要加分号";"
      

  2.   

    SQL> create or replace procedure ZHUYE as
      2   v_num number(10);
      3  begin --如果表存在则删除
      4    select count(*) into v_num from user_tables t where lower(t.table_name )= 'zhuyecompany';
      5    if v_num=1 then
      6       execute immediate ' drop table zhuyecompany';
      7    end if;
      8  exception when others then
      9  null;
     10  end;
     11  /
     
    Procedure created
     
    SQL> create table zhuyecompany( ---创建表
      2  pk_costsubj varchar(50),
      3  costcode varchar(50),
      4  costname varchar(50),
      5  zyjg float, --主业机关
      6  yisc float,
      7  hj float
      8  );
     
    Table created
     
    SQL> exec zhuye;
     
    PL/SQL procedure successfully completed
     
    SQL> select * from zhuyecompany;
     
    select * from zhuyecompany
     
    ORA-00942: table or view does not exist
     
    SQL> 
      

  3.   

    因为表是动态创建的,所以所有的sql都需要动态执行
    create or replace procedure ZHUYE as
      v_num number(10);
    begin
      --如果表存在则删除
      select count(*)
        into v_num
        from user_tables t
       where lower(t.table_name) = 'zhuyecompany';
      if v_num = 1 then
        execute immediate ' drop table zhuyecompany';
      end if;
      execute immediate 'create table zhuyecompany( 
    pk_costsubj varchar(50),
    costcode varchar(50),
    costname varchar(50),
    zyjg float,
    yisc float,   
    hj float)';execute immediate 'create table sum_costsubj as
    select b.settleunitname,f.pk_settleunit,fb.pk_costsubj,sum(plan_money) as plan_money  
    from fp_planbill f left join bd_settleunit b on f.pk_settleunit=b.pk_settleunit
      left join fp_planbill_b fb on f.pk_planbill=fb.pk_planbill
      where 1 = 1 and f.pk_corp = ''1051'' and f.vbillstatus >= ''3''  
    group by b.settleunitname,f.pk_settleunit,fb.pk_costsubj';execute immediate 
      'insert into zhuyecompany
        (pk_costsubj, costcode, costname)
        select pk_costsubj, costcode, costname
          from bd_costsubj
         where pk_corp = ''1051''
           and costcode not in (''5'', ''9'', ''901'', ''902'')
         order by costcode';
    execute immediate 
      'update zhuyecompany
         set zyjg = (select plan_money
                       from sum_costsubj
                      where zhuyecompany.pk_costsubj = sum_costsubj.pk_costsubj
                        and sum_costsubj.settleunitname = ''石家庄东方龙供水有限责任公司'')';
                        
    execute immediate 
      'update zhuyecompany
         set yisc = (select plan_money
                       from sum_costsubj
                      where zhuyecompany.pk_costsubj = sum_costsubj.pk_costsubj
                        and sum_costsubj.settleunitname = ''石家庄东方龙供水有限责任公司一水厂'')';
    execute immediate 
      'update zhuyecompany
         set hj = zyjg + yisc + ersc + sansc + sisc + wusc + liusc + qisc + basc +
                  jxfugs + gxglc + wxgs + jlglzx + jyysbgs + zhjds + yhfwzx +
                  szjcz + gsjls + hqglzx + jsjc + zgyy + ymyey
       where 1 = 1';exception
      when others then
        null;
    end;
      

  4.   

    你上面过程中含有DDL语句
    存储过程中DDL语句要用execute immediate 'sql';这样的方式来执行
    或者采用dbms_utility.exec_ddl_statement(sql);
      

  5.   

    要用動態SQL來執行create table語句
      

  6.   


    create or replace procedure ZHUYE as 
    v_count number; 
    begin
    select count(1) into v_count from user_tables where table_name=upper('zhuyecompany');if v_count>0 then
        
    execute immediate ' drop table zhuyecompany';
    elseexecute immediate 'create table zhuyecompany( pk_costsubj varchar(50),costcode varchar(50),
    costname varchar(50),zyjg float, yisc float,hj float)';end if;
    end ;
    --写了一部分 下面的建表你自己在手动运行 或者用上面的也一样 放在else 里面 用动态执行 
      

  7.   

    已经按照3楼说的改了,怎末在 pl sql 中执行 exec zhuye  还是这个提示?
      

  8.   

    当我们使用动态SQL执行DDL,DML语句时,都要用 execute immediate sql_sqlstatement 
    来执行!