2016-01-08 55 views
1

我們可以在存儲過程中將分區作爲參數傳遞嗎?我收到以下錯誤。 請幫忙。我們可以在存儲過程中將分區作爲參數傳遞嗎?我收到以下錯誤

CREATE OR REPLACE PROCEDURE PROCEDURE_NAME (PARTITION_NAME IN VARCHAR2) 
    IS 
    LASTDATE VARCHAR2(12); 
ENDDATE VARCHAR2(12); 
BEGIN 
IF PARTITION_NAME='DEC_2014' OR PARTITION_NAME='JAN_2015' OR PARTITION_NAME='MAR_2015' OR PARTITION_NAME='MAY_2015' OR PARTITION_NAME='JUL_2015' OR PARTITION_NAME='AUG_2015' OR PARTITION_NAME='OCT_2015' OR PARTITION_NAME='DEC_2015' OR PARTITION_NAME='JAN_2016' THEN 
ENDDATE:='31'; 
ELSIF PARTITION_NAME='NOV_2014' OR PARTITION_NAME='APR_2015' OR PARTITION_NAME='JUN_2015' OR PARTITION_NAME='SEP_2015' OR PARTITION_NAME='NOV_2015' THEN 
ENDDATE:='30'; 
ELSE 
ENDDATE:='28'; 
END IF; 
LASTDATE:=CONCAT(CONCAT(ENDDATE,'-'),REPLACE (PARTITION_NAME, '_', '-')); 
DBMS_OUTPUT.PUT_LINE(LASTDATE); 
DBMS_OUTPUT.PUT_LINE(PARTITION_NAME); 

UPDATE 
    /*+ PARALLEL(Alias 4) */ 
    TABLE_NAME PARTITION (PARTITION_NAME) Alias 
SET Alias.Alias_D_EFFECTIVE_DATE = 
    (SELECT 
    /*+ PARALLEL(Alias3 4) */ 
    ALAIS2.ALAIS2_D_DATETIME 
    FROM schema1.TABLE_2 ALAIS2 
    WHERE TRUNC(ALAIS2_D_DATETIME) <= TO_DATE(LASTDATE,'DD-MON-YYYY') 
    AND ALAIS2.ALAIS2_N_TRN_ID      = Alias.Alias_N_PR 
) 
WHERE EXISTS 
    (SELECT Alias3.ALAIS2_D_DATETIME 
    FROM schema1.TABLE_2 Alias3 
    WHERE TRUNC(Alias3.ALAIS2_D_DATETIME) <= TO_DATE(LASTDATE,'DD-MON-YYYY') 
    AND Alias3.ALAIS2_N_TRN_ID       = Alias.Alias_N_PR 
); 

COMMIT; 
END PROCEDURE_NAME; 

錯誤:

錯誤位於第1行 ORA-02149:指定分區不存在 ORA-06512:在 「schema1.PROCEDURE_NAME」,第17行 ORA-06512:在第1行

+5

需要動態SQL - 'EXECUTE IMMEDIATE 'UPDATE /* + PARALLEL(別名4)*/ TABLE_NAME PARTITION(' || PARTITION_NAME ||')別名 SET Alias.Alias_D_EFFECTIVE_DATE = (SELECT .... ';' – Tatiana

+0

基本規則,你不能在編譯的代碼中使用變量來代替IDENTIFIER,在這些情況下需要動態SQL –

+3

另外,考慮這個邏輯來取代笨重的IF語句:)'lastdate:= to_char(last_day (to_date('01_'|| partition_name,'dd_mon_yyyy')),'dd-mon-yyyy'); ' – Ditto

回答

1

我想你可以通過動態SQL語句做,EXECUTE IMMEDIATE語句

請點擊此鏈接... https://docs.oracle.com/cd/B10501_01/appdev.920/a96590/adg09dyn.htm https://docs.oracle.com/cloud/latest/db112/LNPLS/dynamic.htm#LNPLS01115

CREATE OR REPLACE PROCEDURE PROCEDURE_NAME (PARTITION_NAME IN VARCHAR2) 
    IS 
    LASTDATE VARCHAR2(12); 
ENDDATE VARCHAR2(12); 
query_str VARCHAR2(1000); 
BEGIN 
//.......your code....... 
query_str := 'UPDATE TABLE_NAME PARTITION (' || PARTITION_NAME || ') Alias ' 
      ||'SET Alias.Alias_D_EFFECTIVE_DATE =.....' 

EXECUTE IMMEDIATE query_str; 
COMMIT; 
END PROCEDURE_NAME; 

希望這會起作用。

相關問題