pl/sql块的组成:
定义部分:定义变量,常量,游标等 ,可选;
执行部分:实现功能 ;
异常处理部分:处理运行错误,可选;
[DECLARE
declarations] --声明部分
BEGIN
executable statements--可执行部分
[EXCEPTION
handlers]--异常处理部分
END;
示例:
1.
declare
--定义变量
v_rlt number(8) := -5;
i number(3) := 1;
begin
--可执行部分开始
<<fst_loop>> --循环名称
loop
dbms_output.put_line('v_rlt = ' || v_rlt);--控制台输出
i := i + 1;
if mod(i, 3)< 2 then --求余数
v_rlt := v_rlt;
else
v_rlt := v_rlt + 1;
end if;
if v_rlt > 3 then
dbms_output.put_line('变量的值已经大于3,当前值为' || v_rlt);--控制台输出
exit fst_loop;
end if;
end loop fst_loop;--结束loop循环
dbms_output.put_line('LOOP循环已经结束!');
end;--结束pl/sql块
控制台输出:
v_rlt = -5
v_rlt = -4
v_rlt = -4
v_rlt = -4
v_rlt = -3
v_rlt = -3
v_rlt = -3
v_rlt = -2
v_rlt = -2
v_rlt = -2
v_rlt = -1
v_rlt = -1
v_rlt = -1
v_rlt = 0
v_rlt = 0
v_rlt = 0
v_rlt = 1
v_rlt = 1
v_rlt = 1
v_rlt = 2
v_rlt = 2
v_rlt = 2
v_rlt = 3
v_rlt = 3
v_rlt = 3
变量的值已经大于3,当前值为4
LOOP循环已经结束!
--循环插入数据
DECLARE
y_num NUMBER(10):=1;
BEGIN
LOOP
insert into tb_o_change (OIL_CHANGE_ID, C_TYPE, V_ID, T_TIME, OIL_LEVEL, CHANGE_OIL, OIL, GPS_LATITUDE, GPS_LONGITUDE, GPS_ALTITUDE, DIRECTION, SPEED, DRIVER_ID, APP_INST_ID, ROW_CREATER, ROW_CREATE_TIME, ROW_MODIFIER, ROW_MODIFY_TIME, ROW_STATE, ROW_CREATE_APP_INST_ID, ROW_MODIFY_APP_INST_ID)
values (y_num, '000', '77775E6103F54FF688C7523E8A87A966', to_date('01-10-2019 00:00:00', 'dd-mm-yyyy hh24:mi:ss')+y_num/86400, 0.00, 0.00, 250.00, 34.70130200, 113.86037100, 69.00000000, 0.00, null, null, '111ED12E5094CE67DC7DB7493684771E', 'HEHEHE', to_date('01-10-2019 00:00:00', 'dd-mm-yyyy hh24:mi:ss')+y_num/86400, '00000000000000000000000000000000', to_date('01-10-2019 00:00:00', 'dd-mm-yyyy hh24:mi:ss')+y_num/86400, 1, '111ED12E5094CE67DC7DB7493684771E', '111ED12E5094CE67DC7DB7493684771E');
y_num:= y_num + 30;
IF y_num > 2400000 THEN
EXIT;
END IF;
END LOOP;
dbms_output.put_line('成功');
END;