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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值