2

我有以下内容:

create type customer as object (
id number, name varchar2(10), points number,
member procedure add_points(num_points number)
) not final;
/

create type body customer as
member procedure add_points(num_points number) is 
begin
   points := points + num_points;
   commit;
end add_points;
end;
/

create table customer_table of customer;
/

insert into customer_table values (customer(123,'joe',10));
/

然后我这样做是一个匿名块:

declare
cust customer;
begin
select treat(value(c) as customer) into cust from customer_table c where id=123;
c.add_points(100);
end;

但什么也没发生 - 点值保持在 10。

我错过了什么?如果我让我的成员程序成为一个update...set...commit并传递积分和给定的 ID,它就可以工作。

谢谢。

4

1 回答 1

1

您发布的 PL/SQL 无效。我猜你的意思是发布这个:

declare
  cust customer;
begin
  select treat(value(c) as customer) into cust from customer_table c where id=123;
  cust.add_points(100);
end;

即第5行的“cust”不是“c”?

如果是这样,您所做的就是更新变量 cust中的点值,而不是表中的值。你可以看到这样的:

declare
  cust customer;
begin
  select treat(value(c) as customer) into cust from customer_table c where id=123;
  cust.add_points(100);
  dbms_output.put_line(cust.points);
end;

输出:

110

要更新表中的数据确实需要一个 UPDATE 语句。

于 2010-02-22T16:49:58.807 回答