可以在IF()中使用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;
错误是:
错误(22,8):PLS-00103:遇到符号" ctdelete"期待以下之一:=。 (@%;
答案 0 :(得分: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;
如果您只需要将表格中的数据插入另一个表格,则无需进行任何检查,IF
,loop
...您可以这样做:
insert into table2(a, b, c)
select a, b, c
from table1
where ...
答案 1 :(得分: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;
答案 2 :(得分: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;