这个包声明和定义有什么问题

时间:2014-09-22 11:23:28

标签: sql oracle plsql

package cpac 
as
  STGFILE xyz_INSTANCE.FILENAME%TYPE;
  procedure POC (cid     in xyz_instance.client_id%type, 
                 stgtype in xyz_instance.stg_instance%type, 
                 stgsrc  in xyz_instance.stg_source%type);
end;

package body cpac 
as
  procedure POC (cid     in xyz_instance.client_id%type, 
                 stgtype in xyz_instance.stg_instance%type, 
                 stgsrc  in xyz_instance.stg_source%type 
                 ) Is 
   BEGIN
      select filename 
        into stgfile 
        from xyz_instance 
       where stg_instance = stgtype 
         and stg_source = stgsrc 
         and client_id = cid;
   END POC;

begin
  POC('0123','19517','L');
  dbms_output.put_line(STGFILE); 
end cpac;

单独执行SQL语句,单独包含SQL的过程也在执行,但只在包中我得到错误:

  

第4行出错

     

ORA-00900:无效的SQL语句,

参考了以下文件:http://docstore.mik.ua/orelly/oracle/prog2/ch16_02.htm

1 个答案:

答案 0 :(得分:1)

使用CREATE OR REPLACE

为包标题和正文创建添加前缀
CREATE OR REPLACE package cpac 
as
  STGFILE xyz_INSTANCE.FILENAME%TYPE;
  procedure POC (cid     in xyz_instance.client_id%type, 
                 stgtype in xyz_instance.stg_instance%type, 
                 stgsrc  in xyz_instance.stg_source%type);
end;
/

CREATE OR REPLACE package body cpac 
as
  procedure POC (cid     in xyz_instance.client_id%type, 
                 stgtype in xyz_instance.stg_instance%type, 
                 stgsrc  in xyz_instance.stg_source%type 
                 ) Is 
   BEGIN
      select filename 
        into stgfile 
        from xyz_instance 
       where stg_instance = stgtype 
         and stg_source = stgsrc 
         and client_id = cid;
   END POC;

begin
  POC('0123','19517','L');
  dbms_output.put_line(STGFILE); 
end cpac;
/

编辑:如果您没有权限创建包,则无法创建包。但是,您仍然可以在匿名PL / SQL块的DECLARE部分中创建过程或函数,并在块期间稍后运行过程/函数。在下面的示例中,我以无权创建存储过程的用户身份登录,因此尝试创建存储过程时出错:

SQL> create or replace procedure test as begin null; end;
  2  /
create or replace procedure test as begin null; end;
*
ERROR at line 1:
ORA-01031: insufficient privileges


SQL> DECLARE
  2     stgfile   xyz_instance.filename%TYPE;
  3
  4     procedure POC (cid     in xyz_instance.client_id%type,
  5                   stgtype in xyz_instance.stg_instance%type,
  6                   stgsrc  in xyz_instance.stg_source%type
  7                   ) Is
  8     BEGIN
  9        select filename
 10          into stgfile
 11          from xyz_instance
 12         where stg_instance = stgtype
 13           and stg_source = stgsrc
 14           and client_id = cid;
 15     END POC;
 16
 17  begin
 18    POC('0123','19517','L');
 19    dbms_output.put_line(STGFILE);
 20  end;
 21  /
test-filename.txt

PL/SQL procedure successfully completed.