4

所以我正在尝试使用重载,但我遇到了一些麻烦。我的包有 4 个名称相同但参数类型不同(VARCHAR2、NUMBER、BOOLEAN、DATE)的过程。包头和包体在我的问题的最后。

我正在尝试使用这样的包程序:

OPEN bookCountCur;
FETCH bookCountCur INTO how_many;
testutil.reporteq('add procedure, book record count',
  expected_value => '1', actual_value => how_many);
CLOSE bookCountCur;

OPEN copiesCountCur;
FETCH copiesCountCur INTO how_many;
testutil.reporteq('add procedure, book copy record count',
  expected_value => '1', actual_value => how_many);
CLOSE copiesCountCur;

我收到了这个编译错误:

Error(83,5): PLS-00307: too many declarations of 'REPORTEQ' match this call

这是什么意思?


包头:

CREATE OR REPLACE 
PACKAGE TESTUTIL AS 

  /* TODO enter package declarations (types, exceptions, methods etc) here */ 

  PROCEDURE reporteq(description IN VARCHAR2
  , expected_value IN VARCHAR2, actual_value IN VARCHAR2);

  PROCEDURE reporteq(description IN VARCHAR2
  , expected_value IN NUMBER, actual_value IN NUMBER);

  PROCEDURE reporteq(description IN VARCHAR2
  , expected_value IN BOOLEAN, actual_value IN BOOLEAN);

  PROCEDURE reporteq(description IN VARCHAR2
  , expected_value IN DATE, actual_value IN DATE);

END TESTUTIL;

和包体:

CREATE OR REPLACE
PACKAGE BODY TESTUTIL AS

  PROCEDURE reporteq(description IN VARCHAR2
  , expected_value IN VARCHAR2, actual_value IN VARCHAR2) AS
  BEGIN
    DBMS_OUTPUT.PUT(description || ': ');

    IF expected_value = actual_value 
    OR expected_value IS NULL AND actual_value IS NULL
    THEN
      DBMS_OUTPUT.PUT_LINE('PASSED');
    ELSE
      DBMS_OUTPUT.PUT_LINE('FAILED. Expected ' || expected_value || '; got ' || actual_value);
    END IF;
  END reporteq;

  PROCEDURE reporteq(description IN VARCHAR2
  , expected_value IN NUMBER, actual_value IN NUMBER) AS 
  BEGIN
    DBMS_OUTPUT.PUT(description || ': ');

    IF expected_value = actual_value 
    OR expected_value IS NULL AND actual_value IS NULL
    THEN
      DBMS_OUTPUT.PUT_LINE('PASSED');
    ELSE
      DBMS_OUTPUT.PUT_LINE('FAILED. Expected ' || expected_value || '; got ' || actual_value);
    END IF;
  END reporteq;

  PROCEDURE reporteq(description IN VARCHAR2
  , expected_value IN BOOLEAN, actual_value IN BOOLEAN) 
  AS
    expected_value_tostr VARCHAR2(5);
    actual_value_tostr VARCHAR2(5); 
  BEGIN
    DBMS_OUTPUT.PUT(description || ': ');

    IF expected_value = actual_value 
    OR expected_value IS NULL AND actual_value IS NULL
    THEN
      DBMS_OUTPUT.PUT_LINE('PASSED');
    ELSE
      /* Transform TRUE/FALSE to 'TRUE'/'FALSE' */
      IF TRUE = expected_value
      THEN
        expected_value_tostr := 'TRUE';
      ELSE
        expected_value_tostr := 'FALSE';
      END IF;
      IF TRUE = actual_value
      THEN
        actual_value_tostr := 'TRUE';
      ELSE
        actual_value_tostr := 'FALSE';
      END IF;
      DBMS_OUTPUT.PUT_LINE('FAILED. Expected ' || expected_value_tostr || '; got ' || actual_value_tostr);
    END IF;
  END reporteq;

  PROCEDURE reporteq(description IN VARCHAR2
  , expected_value IN DATE, actual_value IN DATE) AS
  BEGIN
    DBMS_OUTPUT.PUT(description || ': ');

    IF expected_value = actual_value 
    OR expected_value IS NULL AND actual_value IS NULL
    THEN
      DBMS_OUTPUT.PUT_LINE('PASSED');
    ELSE
      DBMS_OUTPUT.PUT_LINE('FAILED. Expected ' || TO_CHAR(expected_value, 'YYYY/MM/DD') 
      || '; got ' || TO_CHAR(actual_value, 'YYYY/MM/DD'));
    END IF;
  END reporteq;

END TESTUTIL;
4

2 回答 2

4

您的 PL/SQL 变量how_many是什么类型?它需要是 VARCHAR2,否则 expected_value 需要是 NUMBER。试试这个:

testutil.reporteq('add procedure, book record count',
  expected_value => '1', actual_value => TO_CHAR(how_many));

问题是(假设how_many是数字类型)是您没有与 VARCHAR2 和 NUMBER 参数列表匹配的规范。您有 VARCHAR2/VARCHAR2、NUMBER/NUMBER、VARCHAR2/BOOLEAN 和 DATE/DATE。

于 2011-05-15T18:11:50.520 回答
1

铸造'1'可以VARCHAR2解决问题吗?IE:

testutil.reporteq('add procedure, book record count',
  expected_value => cast('1' as VARCHAR2), actual_value => how_many);

我认为文字可以被PL/SQL 编译器'1'视为 aVARCHAR2或 a NUMBER,甚至可能是 a ...BOOLEAN

于 2011-05-15T17:44:45.087 回答