包含参数的存储过程的新Oracle包

时间:2014-02-25 14:41:58

标签: oracle stored-procedures packages

我在包中编写存储过程时遇到困难。下面是我的存储过程,它在包外部编译得很好,但我认为需要在包体内以不同的方式编写。我承认这是我第一次使用包...

create or replace PROCEDURE SP_COMMENT(P_MEMBER_ID IN VARCHAR2, P_MEMBER_LASTNAME IN 
VARCHAR2, P_MEMBER_FIRSTNAME IN VARCHAR2, P_MEMBER_STARTDATE IN DATE,
P_MEMBER_ENDDATE IN DATE, P_PRODUCT_CAT_CODE IN VARCHAR2, P_COMMENT IN VARCHAR2,    
COMMENT_CURSOR out sys_refcursor) 

AUTHID CURRENT_USER
IS
BEGIN

EXECUTE IMMEDIATE
'INSERT INTO TEST
 (
MEMBER_ID,
MEMBER_LASTNAME,
MEMBER_FIRSTNAME,
MEMBER_STARTDATE,
MEMBER_ENDDATE,
PRODUCT_CAT_CODE,
COMMENTS
 )
 VALUES
 (
p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment)';

commit;   
open COMMENT_CURSOR for select * from sconti.TEST;
END;

以下是我开始使用的包,但不起作用:

 CREATE OR REPLACE 
 PACKAGE COMMENT_TEST IS 

  PROCEDURE SP_COMMENT(P_MEMBER_ID IN VARCHAR2, P_MEMBER_LASTNAME IN VARCHAR2,
  P_MEMBER_FIRSTNAME IN VARCHAR2, P_MEMBER_STARTDATE IN DATE, 
  P_MEMBER_ENDDATE IN DATE, P_PRODUCT_CAT_CODE IN VARCHAR2, P_COMMENT IN VARCHAR2,  
  COMMENT_CURSOR out sys_refcursor) IS
  BEGIN

  EXECUTE IMMEDIATE
  'INSERT INTO TEST
  (
  MEMBER_ID,
  MEMBER_LASTNAME,
MEMBER_FIRSTNAME,
MEMBER_STARTDATE,
MEMBER_ENDDATE,
PRODUCT_CAT_CODE,
COMMENTS
  )
   VALUES
   (
   p_member_id,
   p_member_lastname,
   p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment)';

 commit;   
 open COMMENT_CURSOR for select * from sconti.TEST;
 END; 

  END COMMENT_TEST;

我期待任何回应来帮助我....谢谢!

1 个答案:

答案 0 :(得分:1)

我不能肯定地说(因为你没有分享你得到的错误),但最基本的错误是缺乏对规范/正文的理解。

您已将代码放入包规范中,而不是正文中。规范应该只有程序声明(即没有beginend),因为正文具有程序的全部内容。

虽然它不会影响编译,但还有另一个问题:字符串中的SQL无法访问提供给过程的参数。如果必须使用动态SQL(在这种情况下绝对没有理由),则需要using子句将变量绑定到动态语句中。另外,使SQL静态化将允许在编译时验证SQL语句,这具有明显的优势。

修订后的包装(规格和正文)如下。

CREATE OR REPLACE PACKAGE comment_test IS
   PROCEDURE sp_comment (p_member_id IN VARCHAR2,
                         p_member_lastname IN VARCHAR2,
                         p_member_firstname IN VARCHAR2,
                         p_member_startdate IN DATE,
                         p_member_enddate IN DATE,
                         p_product_cat_code IN VARCHAR2,
                         p_comment IN VARCHAR2,
                         comment_cursor   OUT SYS_REFCURSOR);
END comment_test;
/

CREATE OR REPLACE PACKAGE BODY comment_test IS
   PROCEDURE sp_comment (p_member_id IN VARCHAR2,
                         p_member_lastname IN VARCHAR2,
                         p_member_firstname IN VARCHAR2,
                         p_member_startdate IN DATE,
                         p_member_enddate IN DATE,
                         p_product_cat_code IN VARCHAR2,
                         p_comment IN VARCHAR2,
                         comment_cursor   OUT SYS_REFCURSOR) IS
   BEGIN
      INSERT INTO test (member_id,
                        member_lastname,
                        member_firstname,
                        member_startdate,
                        member_enddate,
                        product_cat_code,
                        comments)
      VALUES      (p_member_id,
                   p_member_lastname,
                   p_member_firstname,
                   p_member_startdate,
                   p_member_enddate,
                   p_product_cat_code,
                   p_comment);

      COMMIT;

      OPEN comment_cursor FOR SELECT * FROM sconti.test;
   END;
END comment_test;
/