我正在尝试编写一个CURSOR来在Oracle中创建一个任务。我不熟悉Oracle中的游标。显然我做错了。有人能告诉我它是什么吗?。
DECLARE
CURSOR c1 IS
SELECT BADGE.EMPID
FROM EVENTS
INNER JOIN BADGE ON EVENTS.CARDNUM = BADGE.ID
INNER JOIN EMP ON BADGE.EMPID = EMP.ID
WHERE EMP.VISITOR = 0
AND EVENTS.EVENT_TIME_UTC - 0.125 > TO_DATE('20090101 00:00:00', 'RRRRMMDD hh24:mi:ss')
AND EVENTS.EVENT_TIME_UTC - 0.125 < TO_DATE('20120101 23:59:59', 'RRRRMMDD hh24:mi:ss');
r1 c1%ROWTYPE;
BEGIN
FOR r1 IN c1 LOOP
UPDATE EVENTS
SET EVENTS.EMPID = r1.EMPID
END LOOP;
END;
感谢您的帮助!
答案 0 :(得分:3)
还有一种方法可以在没有光标的情况下执行此操作,但这是您想要做的。关键是你定义光标然后沿着它循环。识别FOR UPDATE
DECLARE
CURSOR c1 IS
SELECT BADGE.EMPID
FROM EVENTS
INNER JOIN BADGE ON EVENTS.CARDNUM = BADGE.ID
INNER JOIN EMP ON BADGE.EMPID = EMP.ID
WHERE EMP.VISITOR = 0
AND EVENTS.EVENT_TIME_UTC - 0.125 > TO_DATE('20090101 00:00:00', 'RRRRMMDD hh24:mi:ss')
AND EVENTS.EVENT_TIME_UTC - 0.125 < TO_DATE('20120101 23:59:59', 'RRRRMMDD hh24:mi:ss')
FOR UPDATE of events.empid;
BEGIN
for l in c1 loop
UPDATE events
SET empid = l.empid
WHERE CURRENT OF c1;
end loop;
COMMIT;
end;
答案 1 :(得分:2)
不使用隐式游标的解决方案(无锁定问题):
Begin
For X in (
SELECT BADGE.EMPID, EVENTS.CARDNUM FROM EVENTS
INNER JOIN BADGE ON EVENTS.CARDNUM = BADGE.ID
INNER JOIN EMP ON BADGE.EMPID = EMP.ID
WHERE EMP.VISITOR = 0 AND
EVENTS.EVENT_TIME_UTC - 0.125 > TO_DATE('20090101 00:00:00', 'RRRRMMDD hh24:mi:ss') AND
EVENTS.EVENT_TIME_UTC - 0.125 < TO_DATE('20120101 23:59:59', 'RRRRMMDD hh24:mi:ss')
)
Loop
UPDATE EVENTS
SET EVENTS.EMPID = X.EMPID
WHERE EVENTS.CARDNUM = X.CARDNUM;
END LOOP;
COMMIT;
END;