create or replace package p_view_param  is 
   function set_param(num VARCHAR2) return VARCHAR2; 
   function get_param  return VARCHAR2;  
end p_view_param; 
CREATE  or replace package body p_view_param is 
       paramValue VARCHAR2; 
       -- Param
       function set_param(num VARCHAR2) return VARCHAR2 is 
       begin 
         paramValue:=num; 
         --dbms_output.put_line('num'||num);
         return num; 
        end;  
       function get_param return VARCHAR2 is 
       begin 
        dbms_output.put_line('paramValue'||paramValue);
         return paramValue; 
       end; 
END  p_view_param; 
 BEGIN
     SELECT p_view_param.set_param('sss')AS temp  FROM dual;
 END;
View Code

执行时发现错误:

这段sql主要两个问题:

1.参数变量名尽量不要num这些特殊意义的单词;

2.包里面的方法是独立的个体,包只是根据相关功能把函数和存储过程封装在一起罢了,而函数之间是独立的,不存在共享变量之类的。如果确实需要就设计为参数传递即可。

3.创建包和创建包体之间分开,不要一起创建。否则创建包的时候会把包体带进去。

修正后的sql如下:

create or replace package p_view_param  is
   function set_param(paramVal VARCHAR2) return VARCHAR2;
   function get_param(paramVal VARCHAR2)  return VARCHAR2;
end p_view_param;
create or replace package body p_view_param is
       -- Param
       function set_param(paramVal VARCHAR2) return VARCHAR2 is
        paramValue VARCHAR2(200);
       begin
         paramValue:=paramVal;
         return paramValue;
        end;
       function get_param(paramVal VARCHAR2)  return VARCHAR2 is        
       begin
              --dbms_output.put_line('paramValue'||paramValue);
         return paramVal;
       end;
END  p_view_param;
 SELECT p_view_param.set_param('sss') as temp FROM dual;
 SELECT p_view_param.get_param(p_view_param.set_param('sss')) as temp FROM dual;
View Code