0

我有一个带有以下描述的表产品:

desc products;

 Name                  Null?    Type
---------------------- -------- --------------
 PID                   NOT NULL CHAR(4)
 PNAME                          VARCHAR2(15)
 PRICE                          NUMBER(6,2)
 DISCNT_RATE                    NUMBER(3,2)

我的游标语句如下:

declare
  cursor c5 is
    select pid, pname, price
      from products
    c5_rec c5%rowtype
      group by price for update;

begin
  for c5_rec in c5
  loop
    if(c5_rec.price > 100) then
      delete from products where current of c5;
    end if;
  end loop;
end;
/

当我在第 4 行和第 5 行中不包含 group by 和 rowtype 时,它​​给了我错误:

integrity constraint (MYID.SYS_C0012393) violated - child record found ORA06512: at line 7

我要做的是编写一个PL/SQL包含游标的匿名块GROUP BY,然后在执行部分中对游标的结果表执行更新/删除。在哪里可以添加 group by 子句?我哪里错了?

4

1 回答 1

0

选择光标中的所有列,因为“c5_rec”的数据类型与表中的列数和列的数据类型相同。

cursor c5 is
select pid, pname, price, discnt_rate from products

或者,您可以将“c5_rec”数据类型更改为与价格列相同。

cursor c5 is
select price from products group by price;
c5_rec c5%products.price%TYPE;

您也可以这样做,当您需要超过 1 列但不是所有列时,它很灵活。

declare 
--cursor c5 is
--select pid, pname, price from products
--c5_rec c5%rowtype
--group by price for update;
begin
for c5_rec in (select price from products group by price)
loop
if(c5_rec.price > 100) then
delete from products
where price = c5_rec.price;
end if;
end loop;
end;
于 2015-11-09T03:26:06.797 回答