PL / SQL:ORA-00947:创建函数时没有足够的值

时间:2017-01-18 17:16:03

标签: oracle plsql oracle12c

我正在创建一个包含返回表的函数的包。这是(简化)代码:

    Iterator<MyTransaction> iterator = transactions.iterator();
    while(iterator.hasNext()) {
        MyTransaction tx = iterator.next();
        if (transactionClasses.containsKey(tx.baseClassName)) {
            iterator.remove();
        }
    }

编译程序包时出现此错误:

DROP TYPE A_TABLE;
 /
CREATE OR REPLACE TYPE A_RECORD IS object (
    ID  varchar(10),
    Name    varchar(500)
);
/ 
CREATE  TYPE A_TABLE IS TABLE OF A_RECORD;
/
CREATE OR REPLACE PACKAGE My_Package AS

  results A_TABLE;

  FUNCTION A_Data() 
  RETURN A_TABLE;

END My_Package;
/
CREATE OR REPLACE PACKAGE BODY My_Package IS 

  FUNCTION A_Data() RETURN A_TABLE IS results A_TABLE;

  BEGIN

    SELECT ID,Name
    BULK COLLECT INTO results 
    FROM Customer_Table customer

  RETURN results;

  END A_Data;

END My_Package;
/

虽然目前正在处理数据库,但当前没有数据存在,但查询本身仍然有效。我不明白为什么这会破裂。我认为它可能与类型有关?

2 个答案:

答案 0 :(得分:1)

问题是

SELECT ID,Name  -- here
BULK COLLECT INTO results 
FROM Customer_Table customer

需要在结果中收集A_RECORD类型的对象。所以,应该打电话

SELECT A_RECORD(ID,Name) BULK ...`

此外,我认为你并不需要在外部创建类型。这应该有效:

create or replace PACKAGE My_Package AS
  TYPE A_TABLE IS TABLE OF Customr_Table%rowtype;

  FUNCTION A_Data
  RETURN A_TABLE;

END My_Package;
/

CREATE OR REPLACE PACKAGE BODY My_Package
IS
FUNCTION A_Data
  RETURN A_TABLE
IS
  results A_TABLE;
BEGIN
  SELECT ID, NAME BULK COLLECT INTO results FROM Customr_Table;
  RETURN results;
END A_Data;
END My_Package;
/

你可以像这样使用它:

declare
  v My_Package.A_Table;
begin
  v := My_Package.A_data();
  for i in 1 .. v.count loop
    dbms_output.put_line(v(i).id || ' ' || v(i).Name);
  end loop;
end;
/

答案 1 :(得分:1)

您可以在一个记录中选择两个值。它不是这样的。您应该在选择中创建记录类型:

CREATE OR REPLACE PACKAGE BODY My_Package IS 

  FUNCTION A_Data() RETURN A_TABLE IS results A_TABLE;

  BEGIN

    SELECT A_RECORD(ID,Name)
    BULK COLLECT INTO results 
    FROM Customer_Table customer

  RETURN results;

  END A_Data;

END My_Package;
/