2

我正在尝试编写一个 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;  

谢谢你的帮助!

4

2 回答 2

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;
于 2013-06-03T15:16:05.967 回答
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;  
于 2013-06-03T16:58:41.283 回答