PL / SQL - 删除FOR IN LOOP中的当前记录

时间:2017-05-23 23:44:29

标签: sql oracle plsql sql-delete for-in-loop

我试图弄清楚如何使用FOR .. IN .. LOOP

删除记录
FOR REC IN (SELECT * FROM WORKLIST) LOOP
  GET_DATA(REC);
  --DELETE REC FROM WORKLIST
END LOOP;

GET_DATA过程将每一行爆炸为多个,因此当前的一行变得过时。我试图删除当前的记录。在这种情况下,不确定是否可以使用REC对象。

可悲的是,WORKLIST没有任何钥匙。

2 个答案:

答案 0 :(得分:3)

在光标上使用current of

DECLARE
CURSOR c1 IS 
    SELECT * from worklist FOR UPDATE <any column name>; 
BEGIN 
      open c1; 
      loop 
          fetch c1 into crow; 
          exit when c1%notfound;  
          DELETE FROM worklist WHERE CURRENT OF c1; 
      end loop;        
      COMMIT; 
      close c1; 
END;

答案 1 :(得分:1)

如果WORKLIST有主键,只需使用它:

DELETE FROM WORKLIST WHERE <PK> = REC.<PK>

如果没有主键我会尝试:

DECLARE
  vTmp schema.worklist%rowtype;
BEGIN
  FOR rec IN (SELECT rowid, w.* FROM WORKLIST w) LOOP
    vTmp := null;
    vTmp.col1 := rec.col1;
    vTmp.col2 := rec.col2;
    -- And so on ...
    GET_DATE(vTmp);
    DELETE FROM WORKLIST WHERE rowid = rec.rowid;
  END LOOP;
END;