PLS-00306 的未知原因:参数数量或类型错误

Unknown reason for PLS-00306: wrong number or types of arguments

我在使用简单 PL/SQL 时遇到了问题。我将 table 定义如下:

TYPE tr_GatheredData IS RECORD(
BS_ID          number,
CUSTOMER_NAME  varchar2(200),
MONTH          varchar2(30),
YEAR           number,
LEAKAGE        number);

TYPE tt_GatheredData IS TABLE OF tr_GatheredData;

之后我尝试像这样启动一个变量:

results := tt_GatheredData(1, 'lol', 'omg', 2, 3);

我觉得一切都是正确的,但我明白了

Error(8,16): PLS-00306: wrong number or types of arguments in call to 'TT_GATHEREDDATA'

对每一个compliation。你们能告诉我问题出在哪里吗?

甲骨文 18c qualified expression:

DECLARE
  TYPE tr_GatheredData IS RECORD(
     BS_ID          number,
     CUSTOMER_NAME  varchar2(200),
     MONTH          varchar2(30),
     YEAR           number,
     LEAKAGE        number);

  TYPE tt_GatheredData IS TABLE OF tr_GatheredData;
  results tt_GatheredData;
BEGIN
   results := tt_GatheredData(tr_GatheredData(1, 'lol', 'omg', 2, 3));
   DBMS_OUTPUT.PUT_LINE(results(1).Customer_name);
END;
//

以前的版本:

DECLARE
TYPE tr_GatheredData IS RECORD(
BS_ID          number,
CUSTOMER_NAME  varchar2(200),
MONTH          varchar2(30),
YEAR           number,
LEAKAGE        number);
TYPE tt_GatheredData IS TABLE OF tr_GatheredData;
  results tt_GatheredData;
  t tr_GatheredData ;
BEGIN
   t.BS_ID := 1;
   t.CUSTOMER_NAME := 'lol';
   t.MONTH := 'omg';
   t.YEAR := 2;
   t.LEAKAGE := 3;
   results := tt_GatheredData(t);  -- element has to be record type

   DBMS_OUTPUT.PUT_LINE(results(1).Customer_name);
END;
//

db<>fiddle demo

tt_GatheredDatatr_GatheredData 条记录的集合,因此您需要将其中的一组传递给它,而不是 (1, 'lol', 'omg', 2, 3).

正如 Lukasz 的回答所提到的,PL/SQL Oracle 18c 之前的记录类型只是简单的多值变量,所以如果你想要一个方便的构造函数,你必须自己编写,或者声明每个 tr_GatheredData 记录并显式地为其赋值。

集合类型确实带有隐式构造函数,或者您可以根据需要一次显式分配一个元素。

这是一种方法,使用独立的(18c 之前的)记录构造函数一次性构建包含多个元素的集合:

declare
    type tr_GatheredData is record
    ( bs_id          number
    , customer_name  varchar2(200)
    , month          varchar2(30)
    , year           number
    , leakage        number );

    type tt_GatheredData is table of tr_GatheredData;
    results tt_GatheredData;

    function new_gathereddata
        ( p_bs_id          number
        , p_customer_name  varchar2
        , p_month          varchar2
        , p_year           number
        , p_leakage        number )
        return tr_GatheredData
    is
        r tr_GatheredData;
    begin
        r.bs_id          := p_bs_id;
        r.customer_name  := p_customer_name;
        r.month          := p_month;
        r.year           := p_year;
        r.leakage        := p_leakage;

        return r;
    end new_gathereddata;

begin
    results :=
        tt_GatheredData
        ( new_gathereddata(1, 'aaa', 'January',  1066, 10)
        , new_gathereddata(2, 'bbb', 'February', 1588, 15)
        , new_gathereddata(3, 'ccc', 'March',    1789, 20) );

    dbms_output.put_line('Collection contains ' || results.count || ' records.');
    dbms_output.put_line('results(3).year = ' || results(3).year);
end;

输出:

Collection contains 3 records.
results(3).year = 1789