1、PL/SQL(Procedure Language/SQL)概述:
指在SQL命令语言中增加了过程处理语句,使sql语言具有过程处理能力。
2、语法:
[declare]
---声明部分(变量,光标,例外)
begin
--pl/sql程序体
[exception]
--处理异常
end;
3、定义变量及常量
定义number变量,定义PI常量,定义记录型变量,定义引用型变量
declare
i number; --变量赋值
PI constant number := 3.14; --定义常量
pemp emp%rowtype; --记录型变量
pname emp.ename%type; --引用型变量
begin
i := 1;
--PI := PI+1;
dbms_output.put_line(i);
dbms_output.put_line(PI);
select * into pemp from emp where empno = 7788;
dbms_output.put_line('编号:' ||pemp.empno || '姓名:'|| pemp.ename);
select ename into pname from emp where empno = 7788;
dbms_output.put_line('姓名:'|| pname);
end;
执行结果:
4、If语句
语法:
if判断分支语法:
begin
if 条件判断 then
elsif 条件判断 then
else
end if;
end;
案例1:
从控制台输入一个数字,如果数字是1,则输出我是1
declare
i number := #
begin
if i = 1 then
dbms_output.put_line('我是1');
else
dbms_output.put_line('我不是1');
end if;
end;
案例2:
如果输入的年龄在18岁以下,输出未成年人,18~40:成年人,40以上老年人
declare
i number := &i;
begin
if i < 18 then
dbms_output.put_line('未成年人');
elsif i >= 18 and i < 40 then
dbms_output.put_line('成年人');
else
dbms_output.put_line('老年人');
end if;
end;
5、loop循环
语法:
语法1:
while 循环条件 loop
end loop;
语法2:(常用)
loop
exit when 退出循环条件;
end loop;
语法3:
for i in 1..10 loop
案例1:
语法1实现在控制台输出1~10
declare
i number := 1;
begin
while i <= 10 loop
dbms_output.put_line(i);
i:= i+1;
end loop;
end;
案例2:
语法2实现在控制台输出1~10(掌握)
declare
i number :=1;
begin
loop
exit when i > 10;
dbms_output.put_line('第二种'||i);
i := i+1;
end loop;
end;
案例3:
语法3实现在控制台输出1~10
begin
for num3 in 1..10 loop
dbms_output.put_line('第三种'||num3);
end loop;
end;
6、光标
概述:就是在pl/sql编程中结果集的概念
语法:
cursor 光标名称 is select查询语句;
提取游标:
open 游标名称;
loop
fetch 游标名称 into 变量;
exit when 游标%notfound;
end loop;
close 游标名称;
案例1:使用光标输出emp表中7369的员工信息
declare
cursor emp_cur is select * from emp where empno = 7369;
pemp emp%rowtype;
begin
open emp_cur;
fetch emp_cur into pemp;
dbms_output.put_line('编号:' ||pemp.empno || '姓名:'|| pemp.ename);
close emp_cur;
end;
案例2:
输出指定部门的员工信息
declare
cursor emp_cur(dno number) is select * from emp where deptno = dno;
pemp emp%rowtype;
begin
open emp_cur(20);
loop
exit when emp_cur%notfound;
fetch emp_cur into pemp;
dbms_output.put_line('编号:' ||pemp.empno || '姓名:'|| pemp.ename);
end loop;
close emp_cur;
end;
案例3:给部门编号为10的员工涨工资
declare
cursor emp_cur(dno number) is select * from emp where deptno = dno;
pemp emp%rowtype;
begin
open emp_cur(10);
loop
fetch emp_cur into pemp;
exit when emp_cur%notfound;
update emp set sal = sal + 10000 where empno = pemp.empno;
dbms_output.put_line('编号:' ||pemp.empno || '姓名:'|| pemp.ename);
end loop;
commit;
close emp_cur;
end;
select
*
from
emp
where
deptno = 10;
7、例外
(1)系统异常,被0除的zero_divide异常
declare
i number := 1;
begin
i := i / 0;
exception
when zero_divide then
dbms_output.put_line('不能除以0');
end;
(2)
系统异常,设置错误的value_error异常
declare
i number := 1;
begin
i := 'hello';
exception
when value_error then
dbms_output.put_line('赋值错误');
end;
(3)
系统异常,都可以通过others异常捕获
declare
i number := 1;
begin
i := 'hello';
exception
when others then
dbms_output.put_line('有错误');
end;
(4)
自定义异常,查询部门编号为50员工(自定义exception)
declare
cursor emp_cur is select * from emp where deptno = 50;
pemp emp%rowtype;
no_date exception;
begin
open emp_cur;
fetch emp_cur into pemp;
if emp_cur%notfound then
raise no_date;
end if;
close emp_cur;
exception
when no_date then
dbms_output.put_line('没有查到数据');
end;