匿名块游标包括group by和for update

时间:2015-11-09 01:55:07

标签: sql oracle plsql cursor ora-06512

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

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子句?我哪里错了?

1 个答案:

答案 0 :(得分: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;