IF() 内的游标 Then

Cursor inside IF() Then

是否可以在 IF() Then 条件中使用 Cursor?我试过下面的代码 但它正在工作..有人帮我解决这个问题吗?

我的密码是:

BEGIN
IF EXISTS 
((select '1'
  from cttest c
 where not exists( 
 select 1  from cof o where  c.createddate > add_months(sysdate,-6) and c.ctid not in (
 o.ctid , o.bctid, o.lc1, o.lc2, o.sslc1,o.sslc2) 
 ) and c.lastupdated is null and c.lastupdatedcof is null)) THEN
begin
cursor ctdelete 
IS
 select ctid,ctname  from cttest c
 where not exists( 
 select 1  from cof o where  c.createddate > add_months(sysdate,-6) and c.ctid not in (
 o.ctid , o.bctid, o.lc1, o.lc2, o.sslc1,o.sslc2) 
 ) and c.lastupdated is null and c.lastupdatedcof is null
end;
FOR reDel_audit IN ctdelete
  LOOP 
 insert into ctaudit (ctid,ctname,v_IsDeleted,null,sysdate); 
COMMIT; 
END LOOP;
END;

错误是:

Error(22,8): PLS-00103: 遇到以下符号之一时遇到符号 "ctdelete": := 。 ( @% ;

您不能以这种方式评估记录的存在。

一种方法是使用变量存储查询结果,然后在 IF 中计算变量;例如:

create table test(a) as (
    select 1 from dual union all
    select 2 from dual
)    

declare
    vCount  number;
begin
    select count(1)
    into vCount
    from test; 
    --
    if vCount > 0 then
        dbms_output.put_line(vCount || ' rows found');
    else
        dbms_output.put_line('No rows found');
    end if;
    --
    for i in ( 
                select a
                from test
             )
    loop
        dbms_output.put_line('Value: ' || i.a);
    end loop; 
end;

如果您只需要将数据从 table 插入到另一个,则不需要任何检查,IFloop...您可以简单地执行以下操作:

insert into table2(a, b, c)
select a, b, c
from table1
where ...

这样写怎么样?只需遍历光标并将找到的每个行标记为已删除。

BEGIN
   CURSOR ctdelete IS
      SELECT ctid, ctname
        FROM cttest c
       WHERE NOT EXISTS
       (SELECT 1
                FROM cof o
               WHERE c.createddate > add_months(SYSDATE, -6)
                 AND c.ctid NOT IN
                     (o.ctid, o.bctid, o.lc1, o.lc2, o.sslc1, o.sslc2))
         AND c.lastupdated IS NULL
         AND c.lastupdatedcof IS NULL;

   FOR redel_audit IN ctdelete
   LOOP
      INSERT INTO ctaudit
         (redel_audit.ctid, redel_audit.ctname, 'Y', NULL, SYSDATE);
      COMMIT;
   END LOOP;
END;

我没有使用光标而是这样做的:它工作正常..感谢大家的宝贵回复..我试了很多..谢谢

BEGIN
    DBMS_OUTPUT.PUT_LINE('Contact Delete');
    SELECT COUNT(ctid)
    INTO v_count
    FROM cttest c
    WHERE NOT EXISTS
      (SELECT 1
      FROM cof o
      WHERE c.createddate > add_months(sysdate,-6)
      AND c.ctid NOT                          IN ( o.ctid , o.bctid, o.lc1, o.lc2, o.sslc1,o.sslc2)
      )
    AND c.lastupdated    IS NULL
    AND c.lastupdatedcof IS NULL;
    IF v_count            >0 THEN
      DBMS_OUTPUT.PUT_LINE('Count==>'||v_count);
      DBMS_OUTPUT.PUT_LINE('Deleted Status==>'||v_IsDeleted);
      INSERT
      INTO ctaudit
        (
          ctid,
          ctname,
          isdeleted,
          ismasked,
          updatedon
        )
        (SELECT ctid,
            ctname,
            'Y'     AS isdeleted,
            NULL    AS ismasked,
            SYSDATE AS updatedon
          FROM cttest c
          WHERE NOT EXISTS
            (SELECT 1
            FROM cof o
            WHERE c.createddate > add_months(sysdate,-6)
            AND c.ctid NOT                          IN ( o.ctid , o.bctid, o.lc1, o.lc2, o.sslc1,o.sslc2)
            )
          AND C.Lastupdated    IS NULL
          AND C.Lastupdatedcof IS NULL
        );
    END IF;
    DELETE
    FROM cttest c
    WHERE NOT EXISTS
      (SELECT 1
      FROM cof o
      WHERE c.createddate   > add_months(sysdate,-6)
      AND c.ctid NOT                            IN ( o.ctid , o.bctid, o.lc1, o.lc2, o.sslc1,o.sslc2)
      AND c.lastupdated    IS NULL
      AND c.lastupdatedcof IS NULL
      );
    COMMIT;
    DBMS_OUTPUT.PUT_LINE('Compelted.......');
  END;