PLSQL编程:procedure Language 过程语言 Oracle对SQL的一个扩展
让我们能够让像在Java当中一样写if else else if
条件,还可以编写相应的循环逻辑 for while
declare
-- 声明变量
变量名 变量类型;
变量名 变量类型 := 初始值;
begin
-- 编写业务逻辑
end;
dbms_output.put_line();相当于Java当中的System.out.printf("");
declare
i varchar2(10) := '张三';
begin
dbms_output.put_line(i);
end;
vsal emp.sal%type; --引用型的变量
declare
vsal emp.sal%type;
begin
select sal from emp where empno = 7369;
end;
vrow emp%rowtype; -- 声明记录型变量
declare
vrow emp%rowtype; -- 行类型一行记录
begin
select * into vrow from emp where empno = 7369;
dbms_output.put_line('姓名:'|| vrow.ename || '工资' || vrow.sal);
end;
declare
age number := 20;
begin
if age < 18 then
dbms_output.put_line('小屁孩');
elsif age >= 18 and age <= 24 then
dbms_output.put_line('年轻人');
elsif age > 24 and age < 40 then
dbms_output.put_line('中年人');
else
dbms_output.put_line('老年人');
end if;
end;
declare
age number := &aaa;
begin
if age < 18 then
dbms_output.put_line('小屁孩');
elsif age >= 18 and age <= 24 then
dbms_output.put_line('年轻人');
elsif age > 24 and age < 40 then
dbms_output.put_line('中年人');
else
dbms_output.put_line('老年人');
end if;
end;
while
循环while 条件 loop
end loop;
declare
i number := 1;
begin
while i <= 10 loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
for
循环:for 变量 in 起始值.. 结束值 loop
end loop;
declare
begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
end;
declare
begin
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop;
end;
loop
循环:loop
exit when 条件
end
loop;
declare
i number :=1;
begin
loop
exit when i > 10;
dbms_output.put_line(i);
i := i + 1;
end loop;
end;
*
号输出菱形
*
***
*****
***
*
declare
m number := 2;
begin
for x in -m..m loop
for y in -m..m loop
if abs(y) + abs(x) <= m then
dbms_output.put('*');
else
dbms_output.put(' ');
end if;
end loop;
dbms_output.new_line();
end loop;
end;
-- 使用PLSQL输出三角形,只要是三个角
declare
m number := 20;
begin
for x in -m..m loop
for y in -m..m loop
if abs(y) + abs(x) <= m and x<=0 then
dbms_output.put('*');
else
dbms_output.put(' ');
end if;
end loop;
dbms_output.new_line();
end loop;
end;