2016-09-30 39 views
-2

可以在IF()內使用Cursor Then條件?我試着下面的代碼 但它的工作..任何人都幫我解決這個問題?IF()內的光標然後

我的代碼是:

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; 

錯誤是:

錯誤(22,8):PLS-00103:出現符號 「ctdelete」 在需要下列之一時::=。 (@%;

+1

你甚至需要第一光標?您的FOR LOOP將評估記錄的存在。 – Drumbeg

+0

@Drumbeg基於遊標的結果,我需要遍歷每一行並插入到ctaudit表中。 –

+1

那麼,遊標看起來幾乎和我一樣。不要以爲你需要根據相同的標準在循環之前檢查記錄的存在。 – Drumbeg

回答

0

無法評估記錄是否存在以這種方式

的一種方法是使用一個變量來存儲查詢的結果,然後在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; 

如果你只需要從一個表到另一個插入數據,你不需要任何檢查,IFloop ...你可以簡單地做:

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

我需要知道如何在for循環中使用遊標 –

+0

您不需要遊標,可以以更簡單的方式使用循環。剛剛編輯顯示一個例子 – Aleksej

0

這樣寫呢?只需遍歷遊標並將每個找到的行標記爲已刪除。

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; 
0

而不是使用遊標我不喜歡這樣的:它的正常工作..感謝所有的寶貴的答覆..我試過很多..謝謝

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;