SQL 循环执行及间隔多长时间后执行

xxx

---------------------------------------------------


create table T_CONTINUE_WORK_TEST
(
  TEST_YEAR  NUMBER(4),
  TEST_MONTH NUMBER(2)
);

select * from t_continue_work_test;

truncate table t_continue_work_test;


--倒循环
declare
  test_year number;
  test_month number;
begin
  for test_year in reverse 2010 .. 2016 loop
        for test_month in reverse 1 .. 12 loop
          insert into t_continue_work_test
              (test_year, test_month)
            values
              (test_year, test_month);
              commit;
        end loop;
  end loop;
end;
/

--正循环
declare
  test_year number;
  test_month number;
begin
  for test_year in 2010 .. 2013 loop
        for test_month in 1 .. 12 loop
          insert into t_continue_work_test
              (test_year, test_month)
            values
              (test_year, test_month);
              commit;
        end loop;
        DBMS_LOCK.SLEEP(10);--每10秒插一次
  end loop;
end;
/


---------------------------------------------------

xxx

--通过while 实现
declare
  test_year  int;
  test_month int;
begin
  test_year  := 2010;
  while test_year <= 2014 loop
    test_month := 01;
    while test_month <= 12 loop
      insert into t_continue_work_test
        (test_year, test_month)
      values
        (test_year, test_month);
      commit;
      test_month := test_month + 1;
    end loop;
    test_year := test_year + 1;
  end loop;

end;
/

xxx

--1、ORACLE中的GOTO用法
declare
  x number;
begin
  x := 9;
  <<repeat_loop>> --循环点
  x := x - 1;
  dbms_output.put_line(x);
  if x > 0 then
    goto repeat_loop; --当x的值小于9时,就goto到repeat_loop
  end if;
end;
/


--2、ORACLE中的FOR循环用法
declare
  x number; --声明变量
begin
  x := 1; --给初值
  for x in reverse 1 .. 10 loop
    --reverse由大到小
    dbms_output.put_line('内:x=' || x);
  end loop;
  dbms_output.put_line('end loop:x=' || x); --x=1
end;
/

--3、ORACLE中的WHILE循环用法
declare
  x number;
begin
  x := 0;
  while x < 9 loop
    x := x + 1;
    dbms_output.put_line('内:x=' || x);
  end loop;
  dbms_output.put_line('外:x=' || x);
end;
/


--4、ORACLE中的LOOP循环用法
declare
  x number;
begin
  x := 0;
  loop
    x := x + 1;
  
    exit when x > 9;
    dbms_output.put_line('内:x=' || x);
  end loop;
  dbms_output.put_line('外:x=' || x);
end;
/

xxx

猜你喜欢

转载自guocc.iteye.com/blog/2277170