Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

sql - Create a package with only specification?

The code listed below creates a package with only a specification. I keep getting an error.

Error:

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

Code:

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code listed below creates a package with only a specification

but your code shows that you are creating a package BODY

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;

A PACKAGE SPECIFICATION

  • 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.

While a PACKAGE BODY

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

If you want to create a package with only specification. You should write it like this:

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;

Just to warn you, you have a procedure inside but you are returning a value.This will return an error. You should make sure that the codes you are inserting in a package works to avoid having too many errors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...