DBA基础课程:Oracle PL/SQL变量、常量、流程控制、循环语句
定义变量和常量
[(长度):=];
var_name int;
var_name2 varchar2(20);
var_name3 numeric:=3;
定义常量
constant :=;
con_name constant integer:=3;
流程控制
1.if语句
if then
plsql_sentence;
elseif then
plsql_sentence;
else
plsql_sentence;
end;
2.case语句
case
when then plsql_sentence;
when then plsql_sentence;
.......
when then plsql_sentence;
[else plsql_sentence;]
end case;
循环语句
1.loop
loop语句先执行一次循环体,然后再判断exit
when关键字后面的条件表达式的值true还是false,如果是true退出循环体,否则继续执行循环体
loop
plsql_sentence;
exit when end_condition_exp;
end loop;
例子:
declare
sum int := 0;
i int := 0;
begin
loop
i:= i+1;
sum := sum + i;
exit when i= 100;
end loop;
end;
2.while语句
while condition loop
plsql_sentence;
end loop;
3.for语句
for counter in [reverse] min..max loop
plsql_sentence;
end loop;
默认情况下计数器的值会循环递增,reverse关键字使计数器会随着循环器递减
for i in 1..100 loop
dbms_output.put_line(i);
end loop;