执行立即语句以使用游标中的变量创建视图

时间:2015-02-19 20:03:32

标签: oracle plsql

我使用以下代码成功编译但是,当我测试时出现错误:

  

"ORA-00904" "VTX_HR" invalid identifier.

我不太确定我可以这样做,因为我是PLSQL的初学者:

create or replace procedure bal_table
As     

   V_Mondol number;
   V_Emplid varchar2(7);
   V_Monhr number;
   V_name varchar2(50);
   V_erncd varchar2(3);
   V_Grsdol number;
   V_Grshr number;
   VFX_dol number;
   VFX_hr number;
   VSX_dol number;
   VSX_hr number;
   VTX_dol number;
   VTX_hr number; 
   --V_cursor SYS_RefCursor; 
   --V_glapre varchar2(3);                  
   --V_reghr varchar2(1);
   A_Cursor Sys_refcursor;
  -- v_page_record   A_cursor%ROWTYPE;   

    Mmonth number(2):= to_number(to_char(sysdate, 'mm'));
   Mdate number(2) := to_number(to_char(sysdate, 'dd'));
   Mnum number(2);
   Begin
         if Mdate<26 then Mnum := Mmonth -1;
              if mnum<1 then mnum :=12;
              End If;
         Else
                    Mnum := Mmonth;
           End If;



 CASE MNUM
  when 1 then 
        Open A_cursor
        FOR SELECT emplid,name,erncd,Jan_dol as mon_dol,jan_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
        into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
        FROM earnbal where emplid='abc';                                                                                                

  when 2 then 
         Open  A_cursor
         FOR SELECT emplid,name,erncd,Feb_dol as mon_dol,Feb_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
          into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc';                                                                                                

  when 3 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,Mar_dol as mon_dol,Mar_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc'; 

  when 4 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,apr_dol as mon_dol,apr_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc';            

  when 5 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,May_dol as mon_dol,May_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc'; 

  when 6 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,jun_dol as mon_dol,jun_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc';            

  when 7 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,jul_dol as mon_dol,jul_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc';                             

   when 8 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,aug_dol as mon_dol,aug_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc'; 

    when 9 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,sep_dol as mon_dol,sep_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc'; 

    when 10 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,oct_dol as mon_dol,oct_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc';          

      when 11 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,nov_dol as mon_dol,nov_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc'; 

      when 12 then 
         Open A_cursor
         FOR SELECT emplid,name,erncd,dec_dol as mon_dol,dec_hr as mon_hr,grs_dol,grs_hr,fx_dol,fx_hr,sx_dol,sx_hr,tx_dol,tx_hr
         into V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr
         FROM earnbal where emplid='abc';                                       

END CASE;                          


     LOOP   
          FETCH   A_cursor INTO  V_emplid,V_name,V_erncd,V_mondol,V_monhr,V_Grsdol,V_Grshr,VFX_dol,VFX_hr,VSX_dol,VSX_hr,VTX_dol,VTX_hr;
          EXIT WHEN A_Cursor%NOTFOUND;
     End  loop;     
        -- Execute Immediate 'create or replace view moe as select A_CURSOR.v_emplid,A_CURSOR.V_NAME,A_CURSOR.V_ERNCD,A_CURSOR.V_MONDOL,
        -- A_CURSOR.V_MONHR,A_CURSOR.V_GRSDOL,A_CURSOR.V_GRSHR,A_CURSOR.VFX_DOL,A_CURSOR.VFX_HR,A_CURSOR.VSX_DOL,A_CURSOR.VSX_HR,
        -- A_CURSOR.VTX_DOL,A_CURSOR.VTX_HR FROM EARNBAL';
        Execute Immediate 'create table moe as select v_emplid,V_NAME,V_ERNCD,V_MONDOL,
        V_MONHR,V_GRSDOL,V_GRSHR,VFX_DOL,VFX_HR,VSX_DOL,VSX_HR,
       VTX_DOL,VTX_HR FROM EARNBAL ';
       COMMIT;
  --             DBMS_OUTPUT.PUT_LINE('The employee='||V_Emplid||'   '||V_name||'  '||V_erncd||'  '||V_Mondol||'   '|| V_Monhr);
--         End Loop;                                               
End bal_table;

1 个答案:

答案 0 :(得分:1)

问题在于你的execute immediate语句,你应该用这种方式替换它(将值不是变量名传递给字符串,应该执行)。 (即使我没有得到逻辑,为什么你从现有的表中选择那些变量而从那个表中没有任何东西)。

Execute Immediate 'create table moe as select'||  v_emplid||','''||V_NAME||''','||V_ERNCD||','||V_MONDOL||','||
  V_MONHR||','||V_GRSDOL||','||V_GRSHR||','||VFX_DOL||','||VFX_HR||','||VSX_DOL||','||VSX_HR||','||VTX_DOL||','||VTX_HR||' FROM EARNBAL ';

我没有查看所有数据类型,但不要忘记将varchars放在这样的引号中

  ||','''||V_NAME||''',