2015-10-29 46 views
-1

下面列出的代碼創建了一個只有規範的包。我不斷收到錯誤。僅用規格創建包?

錯誤:

Error: PL/SQL: Compilation unit analysis terminated 
Error(1,14): PLS-00201: identifier 'TAXRATE_PKG' must be declared       
Error(1,14): PLS-00304: cannot compile body of 'TAXRATE_PKG' without its specification 

代碼:

CREATE OR REPLACE PACKAGE BODY TAXRATE_PKG IS 

    PROCEDURE state_tax_pf(
        p_state IN VARCHAR2, 
        pv_tax_nc OUT NUMBER, 
        pv_tax_tx OUT NUMBER, 
        pv_tax_tn OUT NUMBER) 
    IS 
     lv_state NUMBER; 
    BEGIN 
     IF p_state = 'NC' THEN 
      pv_tax_nc := 0.35; 
     ELSIF p_state = 'TX' THEN 
      Pv_tax_tx := 0.05; 
     ELSIF p_state = 'TN' THEN 
      pv_tax_tn := 0.02; 
     END IF; 

     RETURN lv_state; 

    END; 
END; 
+1

您需要先創建包。 'CREATE OR REPLACE PACKAGE TAXRATE_PKG IS PROCEDURE state_tax_pf(p_state IN VARCHAR2,pv_tax_nc OUT NUMBER,pv_tax_tx OUT NUMBER,pv_tax_tn OUT NUMBER); END TAXRATE_PKG;'或'CREATE OR REPLACE PACKAGE TAXRATE_PKG is FUNCTION get_state_tax(p_state IN VARCHAR2)RETURN NUMBER; END TAXRATE_PKG;'適當。 (未測試過,沒有Oracle DB方便,可能有其他語法錯誤 –

+0

@ShannonSeverance我將創建一個只包含一個規範的包 – user3508766

+1

從你的create語句中刪除'body','create package BODY'創建而不是規範 –

回答

1

有一些問題與您的代碼。首先,你的錯誤是因爲你正在創建一個PACKAGE而在Oracle中這樣做,你必須首先創建規範。它就像Java中的Interface或文件中的C。因此,對於您的代碼擺脫錯誤的,你要做的:

-- This is the specification 
CREATE OR REPLACE PACKAGE TAXRATE_PKG IS 
    PROCEDURE state_tax_pf(p_state IN VARCHAR2, 
          pv_tax_nc OUT NUMBER, 
          pv_tax_tx OUT NUMBER, 
          pv_tax_tn OUT NUMBER); 
END TAXRATE_PKG; 

--This is the definition 
CREATE OR REPLACE PACKAGE BODY TAXRATE_PKG IS 
    PROCEDURE state_tax_pf(
        p_state IN VARCHAR2, 
        pv_tax_nc OUT NUMBER, 
        pv_tax_tx OUT NUMBER, 
        pv_tax_tn OUT NUMBER) 
    IS 
     lv_state NUMBER; 
    BEGIN 
     IF p_state = 'NC' THEN 
      pv_tax_nc := 0.35; 
     ELSIF p_state = 'TX' THEN 
      Pv_tax_tx := 0.05; 
     ELSIF p_state = 'TN' THEN 
      pv_tax_tn := 0.02; 
     END IF; 

     RETURN lv_state; 

    END; 
END TAXRATE_PKG; 

您在代碼中有其他的問題,更像是邏輯問題

您定義了一個procedure,但您返回的值爲RETURN lv_state;,並且您甚至沒有爲其分配任何值,這沒有任何意義。由於您根本沒有使用它,因此不需要該返回語句或該變量。

的另一個問題是你創建了三個OUT參數(除非該代碼使用,這是測試任何爲空,其他的值),未分配(因爲您if的)你只需要的則一個可以說pv_tax OUT NUMBER

,我看到的另一個問題是,如果你需要的RETURN lv_state;的東西,你必須改變你的procedurefunction和變量分配給什麼。

希望它幫助你理解。

2

The code listed below creates a package with only a specification

,但你的代碼表明你正在創建一個包體

CREATE OR REPLACE PACKAGE 
    BODY TAXRATE_PKG IS -- your code doesnt follow what you are trying to do 

    PROCEDURE state_tax_pf(
        p_state IN VARCHAR2, 
        pv_tax_nc OUT NUMBER, 
        pv_tax_tx OUT NUMBER, 
        pv_tax_tn OUT NUMBER) 
    IS 
     lv_state NUMBER; 
    BEGIN 
     IF p_state = 'NC' THEN 
      pv_tax_nc := 0.35; 
     ELSIF p_state = 'TX' THEN 
      Pv_tax_tx := 0.05; 
     ELSIF p_state = 'TN' THEN 
      pv_tax_tn := 0.02; 
     END IF; 

     RETURN lv_state; 

    END; 
END; 

一個包裝規格

  • contains only the first line of your PROCEDURES/FUNCTIONS,

  • It starts with CREATE OR REPLACE PACKAGE and doesnt include the word "BODY" since its the specification and not the BODY of the package.

雖然PACKAGE BODY

  • contains the BODY of the procedures/functions starting from the word "PROCEDURE" up to the END statement of the procedure.

如果你想創建一個唯一的規格包裝。你應該把它寫這樣的:

CREATE OR REPLACE PACKAGE 
    TAXRATE_PKG IS 

PROCEDURE state_tax_pf(
        p_state IN VARCHAR2, 
        pv_tax_nc OUT NUMBER, 
        pv_tax_tx OUT NUMBER, 
        pv_tax_tn OUT NUMBER) ; 

END TAXRATE_PKG; 

只是爲了提醒你,你有一個內部程序,但你是返回一個價值。將返回一個錯誤。您應該確保您在包裝中插入的代碼能夠避免出現太多錯誤。

相關問題