分类: Oracle
2011-07-29 21:09:20
grant connect to scott;
grant dba to scott;
静态表类型:表类型锁存储的元素的个数固定
type typename is table of 数据类型(varchar2,number,date);
varname typename:=typename(null); //声明变量,初始化
varname.extend(n) //表类型中存放的元素数
varname(i) //访问表类型中的元素
示例:
declare
type emp_tab is table of varchar2(10);
vtab emp_tab:=emp_tab(null);
begin
vtab.extend(10);
select ename into vtab(1) from emp where empno=7521;
select ename into vtab(2) from emp where empno=7499;
for i in 1..vtab.count loop
dbms_output.put_line(vtab(i));
end loop;
end;
declare
type emp_tab is table of emp%rowtype;
vtab emp_tab:=emp_tab(null);
begin
vtab.extend(10);
select * into vtab(1) from emp where empno=7521;
select * into vtab(2) from emp where empno=7499;
for i in 1..vtab.count loop
dbms_output.put_line(vtab(i).ename);
end loop;
end;
动态表类型:表类型中的元素数不固定
type typename is table of 数据类型 index by binary_integer;
varname typename;
declare
type emp_tab is table of varchar2(10) index by binary_integer;//内部增长序号
vtab emp_tab;
begin
select ename bulk collect into vtab from emp;
for i in 1..vtab.count loop
dbms_output.put_line(vtab(i));
end loop;
end;
顺序
选择
if 条件 then
//语句一
else
//语句二
end if;
declare
num1 integer;
num2 integer;
begin
num1:=&1;
num2:=&2;
if num1> num2 then
dbms_output.put_line(num1);
else
dbms_output.put_line(num2);
end if;
end;
if 条件 then
...
elsif 条件 then
...
else
...
end if;
declare
vsal emp.sal%type;
begin
select sal into vsal
from emp
where empno=7521;
if vsal<2000 then
dbms_output.put_line('A');
elsif vsal <3000 then
dbms_output.put_line('B');
elsif vsal <4000 then
dbms_output.put_line('C');
else
dbms_output.put_line('D');
end if;
end;
case 条件
when 值 then 语句;
when 值 then 语句;
when 值 then 语句;
when 值 then 语句;
else 语句
end case;
declare
vchar varchar2(10);
begin
vchar:='&1';
case vchar
when 'a' then dbms_output.put_line('a');
when 'b' then dbms_output.put_line('b');
when 'c' then dbms_output.put_line('c');
else dbms_output.put_line('D');
end case;
end;
循环
loop
exit when 条件
end loop;
declare
i number(4):=1;
vsum number(4):=0;
begin
loop
vsum:=vsum+i;
i:=i+1;
exit when i>100;
end loop;
dbms_output.put_line(vsum);
end;
for varname in 值区间 loop //递增
declare
vsum number(4):=0;
begin
for i in 1..100 loop
vsum:=vsum+i;
end loop;
dbms_output.put_line(vsum);
end;
for varname in reverse 值区间 loop //递减
declare
begin
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop;
end;
while 条件 loop
end loop;
declare
i number(4):=1;
vsum number(4):=0;
begin
while i<=100 loop
vsum:=vsum+i;
i:=i+1;
end loop;
dbms_output.put_line(vsum);
end;
异常
oracle异常分为两种
系统异常
declare
vsal emp.sal%type;
begin
select sal into vsal from emp where empno=9444;
exceptionwhen others then
dbms_output.put_line('工号输入错误');
end;
自定义异常
declare
vage int;
ageexce exception;
begin
vage:=&age;
if vage>100 then
raise ageexce;
end if;
exception when ageexce then
dbms_output.put_line('年龄过大');
end;
游标
一段连续内存空间,存放查询结果集,类似于指针或数组
游标分类
1.隐式游标
系统针对不同的语句创建游标,通过游标名称访问数据,全称游标名sql
游标四个属性
%isopen 打开游标
%found 语句操作是否对数据中的数据产生影响
%notfound
%rowcount 具体影响的数据的数量
begin
delete from emp;
dbms_output.put_line(sql%rowcount);
end;
2.显式游标
游标是存放查询结果,游标后只能用查询语句: select * from tablename;
声明语法: cursor cursorname is select * from tablename//只能是查询语
游标操作步骤
1.open cursorname //将查询的结果存放到指定内存中
2.loop
3.fetch cursoname into 变量
4.exit when cursornamr%notfound;
//判断游标数据是否取完,退出循环
5.end loop;
6.close cursorname
declare
cursor emp_cur is select * from emp;
vrow emp%rowtype;
begin
open emp_cur;
loop
fetch emp_cur into vrow;
exit when emp_cur%notfound ;
dbms_output.put_line(vrow.ename);
end loop;
close emp_cur;
end;
for操作游标特点:
1.自动打开关闭游标,不需要写下面的语句
open emp_cur;
close emp_cur;
2.不需要声明任何变量
declare
cursor emp_cur is select * from emp;
begin
for vrow in emp_cur loop
dbms_output.put_line(vrow.ename);
end loop;
end;
declare
begin
for vrow in ( select * from emp) loop
dbms_output.put_line(vrow.ename);
end loop;
end;
参数游标
参数游标中的参数的数据类型不能指定大小
declare
cursor emp_cur(vdeptno number)//参数不要指定大小
is select * from emp
where deptno=vdeptno;
vvdeptno emp.deptno%type;
begin
vvdeptno:=&no;
for vrow in emp_cur(vvdeptno) loop
dbms_output.put_line(vrow.ename);
end loop;
end;
动态游标:游标所指的内存中可以存放多个查询的数据。动态游标不能用for操作
declare
type emp_ref is ref cursor;
emp_ref_cur emp_ref;
vrow dept%rowtype;
begin
open emp_ref_cur for
select * from dept;
loop
fetch emp_ref_cur into vrow;
exit when emp_ref_cur%notfound;
dbms_output.put_line(vrow.dname);
end loop;
close emp_ref_cur;
end;