【问题标题】:Anonymous block cursor including group by and for update匿名块游标,包括 group by 和 for update
【发布时间】:2015-11-09 01:55:07
【问题描述】:

我有一个表格产品,描述如下:

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

【问题讨论】:

    标签: sql oracle plsql cursor ora-06512


    【解决方案1】:

    选择光标中的所有列,因为“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;
    

    【讨论】:

    • 我试过了,得到了这个错误当期望以下之一时遇到符号“GROUP”:begin function pragma procedure subtype type @Suttipong.P
    • 您是否删除了“按价格分组以进行更新;”线? 1) 它应该在“select pid, pname, price from products”之后,而不应该在“c5_rec c5%rowtype”之后。 2) 您不能在 select 语句中使用“for update”。你能试试我的上一个程序吗?
    猜你喜欢
    • 2012-03-15
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    相关资源
    最近更新 更多