`
jinliyixi
  • 浏览: 17568 次
  • 性别: Icon_minigender_2
  • 来自: 南京
最近访客 更多访客>>
社区版块
存档分类
最新评论

oracle笔记五(类型+循环+异常)

阅读更多
复合类型:
1. 记录类型
a) Type t_record is record(
T_empno emp.empno%type,
T_ename emp.ename%type
T_row emp%rowtype
);
b) rowtype
T_row emp%rowtype := &no;
2. PL/SQL表类型(相当于数组类型)
Type dept_table_type is table of
Dept%rowtype (表中存放的数据类型)  index by binary_integer(指明表的索引,即数组下标);
Index by表中的元素不一定要按任何特定的顺序排序
关键字唯一允许的类型是BINARY_INTERGER   
使用主键值引用表中的字段

3. PL/SQL中的可变数组
Type string is varray(5) of number(4);

PL/SQL中的if…then…(elseif…then…)else…end
if v_sal<1500 then v_comments:='too low';
elsif v_sal<3000 then v_comments:='so so';
else v_comments:='some high';
end if;

PL/SQL中的case语句
v_comments:=
case v_grade
  when 'A' then 'excellent'
  when 'B' then 'very good'
  when 'C' then 'good'
else 'error grade'
end;

PL/SQL 中的LOOP循环
loop
v_count:=v_count+1;
dbms_output.put_line(v_count);

exit when v_count>=10;
end loop;

PL/SQL中的wile循环
while v_count<10 loop
  v_count:=v_count+1;
  dbms_output.put_line(v_count);
end loop;

PL/SQL中的For循环
For v_count in(reverse) 1 。。10 loop
dbms_output.put_line(v_count);
end loop;

PL/SQL中的GoTo语句
for v_count in reverse 1..10 loop
dbms_output.put_line(v_count);
if v_count=5 then goto endogloop; end if;
end loop;
<<endogloop>>
dbms_output.put_line('end ');

PL/SQL中的占位符 null;
<<endogloop>>
Null;

预定义异常处理:
exception
when no_data_found then
  dbms_output.put_line('no data found');
when too_many_rows then
dbms_output.put_line('too many rows');
when others then
dbms_output.put_line(sqlcode ||','||sqlerrm);

非预定义异常处理
Declare
deptno_remaining exception;
pragma exception_init(deptno_remaining,-2292);
begin
excption
when deptno_remaining then
dbms_output.put_line('Î¥•´ÍêÕûÐÔÔ¼Êø£¡');
dbms_output.put_line(sqlcode || ',' ||sqlerrm);
end;

自定义异常处理:
Declare
no_result exception;
begin
update dept set dname='finance' where deptno=v_deptno;
if sql%notfound then
   raise no_result;
end if;
exception
when no_result then
dbms_output.put_line('no databe found');
when others then
dbms_output.put_line(sqlcode || ',' || sqlerrm);
end;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics