使用编译错误创建的类型主体

Type body created with compilation error

customer_ty 我创建的对象有一个嵌套的 table 包括

CREATE TYPE deposit_ty as object(
depNo number,
depCategory ref depcategory_ty,
amount number,
period number
)
/

CREATE TYPE deposit_tbl as table of deposit_ty
/

CREATE TYPE customer_ty as object(
custId varchar2(4),
custName varchar2(10),
address address_ty,
dob date,
deposits deposit_tbl
)
/

我写了一个代码来计算每个客户存入的总金额。这是我写的代码;

alter type customer_ty
add member function totDeposits return number cascade
/

create or replace type body customer_ty as
member function totDeposits 
return number is
total number;
BEGIN
    select sum(d.amount) into total
    from table(self.deposits) d;
    group by self.custId,self.custName
    return total;
END totDeposits;
END;
/

但是我收到一条警告说 "type body created with compilation errors"。我该怎么做才能摆脱这种情况?

如果您在收到 'created with compilation errors' 消息后立即执行 show errors,您将看到如下内容:

LINE/COL ERROR
-------- ------------------------------------------------------------------------------
8/5      PLS-00103: Encountered the symbol "GROUP" when expecting one of the following:

您还可以查询 user_errorsall_errors 视图以查看针对任何 PL/SQL 对象的未决错误。

在这种情况下,这是一个简单的错字;你的分号放错地方了;而不是:

select sum(d.amount) into total
from table(self.deposits) d;
group by self.custId,self.custName

应该是:

select sum(d.amount) into total
from table(self.deposits) d
group by self.custId,self.custName;

在您的版本中,... d; 正在终止 SQL 语句 - 现在该语句无效,因为该截断语句没有 group by 子句;但它并没有抱怨这一点,因为它将 group by ... 视为一个单独的语句,而这并不是任何 PL/SQL 识别为查询、语句、控制循环等的开始.,所以就放弃了。