选择更新后返回refcursor

时间:2014-10-31 14:25:57

标签: oracle plsql oracle11g

我试图通过在Select for update中使用的refcursor返回数据,但我无法找到任何方法。任何人都可以指导我。

CREATE OR REPLACE PROCEDURE SELECT_SCHEDULED_REPORTS
        (o_scheduledreports_cursor OUT SYS_REFCURSOR)

    IS
      CURSOR report_ids
  IS
    SELECT *
    FROM dwp_rep_scheduler_t
    WHERE SCHEDULE_ID IN
      (SELECT SCHEDULE_ID
      FROM
        (SELECT *
        FROM dwp_rep_scheduler_t a
        WHERE status      = 1
        AND schedule_type = 1
        ORDER BY a.start_date
        )
    WHERE ROWNUM <= 5
      ) FOR UPDATE OF status;

    BEGIN         
     FOR report_id IN report_ids
      LOOP
       UPDATE dwp_rep_scheduler_t SET status = 2 WHERE CURRENT OF report_ids;
      END LOOP;
        COMMIT;

-- can I do something like open o_scheduledreports_cursor for report_ids

      END;
    /

正如@Lalit Kumar B所建议的那样,我试过跟随,但现在它编译错误为&#34; PLS-00221:&#39; O_SCHEDULEDREPORTS_CURSOR&#39;不是程序或未定义&#34;

CREATE OR REPLACE PROCEDURE SELECT_SCHEDULED_REPORTS (
   o_scheduledreports_cursor OUT SYS_REFCURSOR)
IS
begin
  open o_scheduledreports_cursor  for
          SELECT *
            FROM dwp_rep_scheduler_t
           WHERE SCHEDULE_ID IN (SELECT SCHEDULE_ID
                                   FROM (  SELECT *
                                             FROM dwp_rep_scheduler_t a
                                            WHERE status = 1 AND schedule_type = 1
                                         ORDER BY a.start_date)
                                  WHERE ROWNUM <= 5)
      FOR UPDATE OF status;

   BEGIN
      FOR report_id IN o_scheduledreports_cursor
      LOOP
         UPDATE dwp_rep_scheduler_t
            SET status = 2
          WHERE CURRENT OF report_ids;
      END LOOP;
      COMMIT;
   END;
END SELECT_SCHEDULED_REPORTS;
/

1 个答案:

答案 0 :(得分:0)

Select for update是一种编程工具,您可以使用它来确保没有其他人更新您的数据。在PL/SQL内,您将使用rows locked for update,然后执行所需的交易。 Commit您的更改,Oracle将发布lock mode 3

你可以这样做,

Open cur for
 select column_list from table where .... 

不是那么具体,但是,关于select for ...update的这个问号链接是一个很好的阅读https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:927629362932

你可能也有兴趣知道关于这个条款的一些有趣的事情,这里是关于select..for update http://lalitkumarb.wordpress.com/2014/09/04/a-myth-about-row-exclusive-table-lock-on-select-for-update-with-no-rows/

的神话

希望它有所帮助!