oracle pl/sql程序

简单的pl/sql程序
declare
begin
dbms_output.put_line('hello world');
end;
什么是PL/SQL?
pl/sql(Procedure language/SQL)
plsql是oracle对sql语言的过程化扩展
指在sql命令语方中增加了过程处理语句(如分支、循环等),使sql语言具有过程处理能力。
 
PL/SQL程序结构
declare
说明部分(变量说明,光标申明,例外说明)
begin
语句序列(DML语句)...
exception
例外处理语句
end;
/
变量和常量说明
说明变量(char, varchar2, date, number, boolean, long)
例如:
a char(15);
b boolean:=true;
c number(8,2);
--myName的类型与emp表中的ename列类型一样
myName emp.ename%type;
--记录型变量
myRec emp%rowtype;
IF语句
IF 条件 THEN 语句1;
语句2;
END IF; IF 条件 THEN 语句序列1;
ELSE 语句序列2;
END IF; IF 条件 THEN 语句;
ELSIF 语句 THEN 语句;
ELSE 语句;
END IF;
--接收键盘输入
--num: 地址值,在该地址上 保存了输入的值
accept num prompt '请输入一个数字'; declare
--定义变量保存输入的数字
pnum number := #
begin
if pnum = 0 then dbms_output.put_line('您输入的是0');
elsif pnum = 1 then dbms_output.put_line('您输入的是1');
elsif pnum = 2 then dbms_output.put_line('您输入的是2');
else dbms_output.put_line('其他数字');
end if;
end;
/

循环语句

WHILE total<100
LOOP
...
total:=total+1;
END LOOP; LOOP
EXIT [when 条件];
...
END LOOP; FOR I IN 1..3
LOOP
语句序列;
END LOOP;
--输出从1-10
declare
num number(10) := 1;
begin
while num <= 10 loop
dbms_output.put_line(num);
num := num + 1;
end loop;
end; --输出从1-10
declare
num number(10) := 1;
begin
loop
exit when num > 10;
dbms_output.put_line(num);
num := num + 1;
end loop;
end;
--输出从1-10
declare
num number(10) := 1;
begin
for num in 1 .. 10 loop
dbms_output.put_line(num);
end loop;
end;
光标(Cursor)==ResultSet
说明光标语法:
CURSOR 光标名 [(参数名 数据类型[,参数名 数据类型]...)]
IS SELECT 语句;
用于存储一个查询返回的多行数据
cursor cr is select ename from emp;
打开光标: open cr;
取一行光标值: fetch cr into pename;
关闭光标:close cr;
(*注意:pename必须与emp表中的ename类型一致。)
declare
--定义光标
cursor cr is select ename, sal from emp;
pname emp.ename%type;
psal emp.sal%type;
begin
--打开光标
open cr;
loop
--退出条件,没有找到记录
exit when cr%notfound;
--取记录
fetch cr into pname, psal;
--打印数据
dbms_output.put_line(pname || '工资是' || psal);
end loop;
--关闭光标
close cr;
end;

带参数的光标

cursor cr(no varchar2)
is select ename,sal from emp where deptno=no;
declare
cursor cr(no number) is select ename,sal from emp where deptno=no;
pname emp.ename%type;
psal emp.sal%type;
begin
--打开光标
open cr(10);
loop
--退出条件,没有找到记录
exit when cr%notfound;
--取记录
fetch cr into pname, psal;
--打印数据
dbms_output.put_line(pname || '工资是' || psal || '涨工资' || (psal+1000));
end loop;
--关闭光标
close cr;
end;

例外

例外是程序设计语言提供的一种功能,用来增强程序的健壮性和容错性。
1、系统定义例外
No_data_found 没有找到数据
Too_many_rows select ... into 语句匹配多个行
Zero_Divide 被零除
Value_error 算术或转换错误
Timeout_on_resource 等待资源发生超时
2、用户定义例外
在declare中定义例外
myExce exception;
在可执行语句中引起例外
raise myExce;
在Exception节处理例外
when myExce then ...
declare
cursor cr is select ename, comm from emp;
pname emp.ename%type;
pcomm emp.comm%type;
myexce exception;
begin
--打开光标
open cr;
loop
--退出条件,没有找到记录
exit when cr%notfound;
--取记录
fetch cr into pname, pcomm;
--引发例外
if pcomm is null then raise myexce;
end if;
dbms_output.put_line(pname || '的comm不是空');
end loop;
--关闭光标
close cr;
exception
--处理例外
when myexce then
dbms_output.put_line(pname || '的comm是空的');
end;
上一篇:每个程序员必须掌握的常用英语词汇


下一篇:Windows10下Apache2.4配置Django