Oracle中采用存储过程的方式批量更新数据

  采用存储过程的方式批量更新数据,Oracle中也可采用merge-update的方式更新,采用批量提交更新方式会更快

DECLARE
  MAX_ROWS            NUMBER DEFAULT 5000;
  ROW_ID_TABLE        DBMS_SQL.UROWID_TABLE;
  DATE_TIME_TABLE      DBMS_SQL.DATE_TABLE;
  CURSOR C1 IS
    SELECT /*+ use_hash(t1,t2) parallel(t1,2) parallel(t2,2) */
     T1.DATE_TIME, T2.ROWID
      FROM TEST_TABLE_1 T1, TEST_TABLE_2 T2
     WHERE T1.TABLE_ID = T2.TABLE_ID
       AND T2.DATE_TIME IS NULL
       AND T1.DATE_TIME IS NOT NULL
       ORDER BY T2.ROWID;
BEGIN
  OPEN C1;
  LOOP
    EXIT WHEN C1%NOTFOUND;
    FETCH C1 BULK COLLECT
      INTO DATE_TIME_TABLE, ROW_ID_TABLE LIMIT MAX_ROWS;
    FORALL I IN 1 .. ROW_ID_TABLE.COUNT
      UPDATE TEST_TABLE_2
         SET DATE_TIME = DATE_TIME_TABLE(I), LCD=SYSDATE
       WHERE ROWID = ROW_ID_TABLE(I);
    COMMIT;
  END LOOP;
  CLOSE C1;
END;
/


你可能感兴趣的:(数据库)