begin和declare

begin
for i in 1..100000
loop
insert into test1 values(i,'a'||i);
end loop;
end;

SQL> select min(id),max(id) from test1;

   MIN(ID)    MAX(ID)
---------- ----------
	 1    1000000

DECLARE
  CURSOR cur IS
    SELECT a.ROWID from test1 a where a.id < 90000;
  V_COUNTER NUMBER;
BEGIN
  V_COUNTER := 0;
  FOR row IN cur LOOP
    UPDATE test1 SET id = 9999 WHERE ROWID = row.ROWID;
    V_COUNTER := V_COUNTER + 1;
    IF (V_COUNTER >= 10000) THEN
      COMMIT;
      V_COUNTER := 0;
    END IF;
  END LOOP;
  COMMIT;
END;



改用begin块:

CREATE OR REPLACE PROCEDURE procs1 AS
begin
DECLARE
  CURSOR cur IS
    SELECT a.ROWID from test1 a where a.id < 90000;
  V_COUNTER NUMBER;
BEGIN
  V_COUNTER := 0;
  FOR row IN cur LOOP
    UPDATE test1 SET id = 9999 WHERE ROWID = row.ROWID;
    V_COUNTER := V_COUNTER + 1;
    IF (V_COUNTER >= 10000) THEN
      COMMIT;
      V_COUNTER := 0;
    END IF;
  END LOOP;
  COMMIT;
END;
end;

SQL> SELECT MIN(ID),MAX(ID) FROM TEST1;

   MIN(ID)    MAX(ID)
---------- ----------
	 1     100000

SQL> EXEC PROCS1;


SQL> SELECT MIN(ID),MAX(ID) FROM TEST1;

   MIN(ID)    MAX(ID)
---------- ----------
	 1     100000

SQL> EXEC PROCS1;

PL/SQL 过程已成功完成。

SQL> SELECT MIN(ID),MAX(ID) FROM TEST1;

   MIN(ID)    MAX(ID)
---------- ----------
      9999     100000

你可能感兴趣的:(PLSQL开发知识)