PL/SQL 基础知识
2014-08-13 00:00
369 查看
摘要: 整理整理(持续更新........)
pl/sql中的四种循环
怎样判断两个值相等(要考虑都是null的情况)
跟踪store procedure or function执行情况
ORACLE异常分为两种类型:系统异常、自定义异常。其中系统异常又分为:预定义异常和非预定义异常。
常见预定义异常如下:
捕获只有错误号,但是没有异常名称的异常
你可以将一个经过命名的异常和一个特别的ORACLE错误相关联。这会使你专门能够捕获此错误,而不是通过WHEN OTHERS处理器来进行捕获。
EXCEPTION_INIT pragma的语法如下:
PRAGMA EXCEPTION_INIT(exception_name,Oracle_error_number);
这里,exception_name是在PRAGMA前面声明的异常的名字,而Oracle_error_number是与此命名异常相关的所需错误代码。这个PRAGMA必须在声明部分.
RAISE_APPLICATION_ERROR和RAISE的比较
RAISE_APPLICATION_ERROR只能被others捕获,不能用已经命名的异常子程序捕获。而RAISE是可以的。RAISE_APPLICATION_ERROR可以填写自己的错误信息,而RAISE不行。
关于Record的一二事
由单行多列的标量构成的复合结构,可以看做是用户自定义的数据类型,将一个或者多个标量封装到一个对象来操作,也可看做是一种临时复合对象类型。
Record可以直接赋值,但是不可以整体比较和整体判断是否为空。
%rowtype和record区别是:
#1,%rowtype的结构是表结构,而record则可以根据实际情况自定义结构。
#2,在insert或者update中,只可以用record的成员,但是可以直接用%rowtype.
关于Cursor的一二事
pl/sql中的四种循环
loop exit when c_page >10; c_page := c_page+1; dbms_output.put_line('c_page : ' || c_page); end loop; while c_page<20 loop c_page := c_page+1; dbms_output.put_line('c_page : ' || c_page); end loop; <<repeat_loop>> dbms_output.put_line('repeat_loop : '); c_page:= c_page - 1; if c_page> 10 then goto repeat_loop; end if; for c_page in reverse 1..100 loop dbms_output.put_line('for loop : ' || c_page); end loop;
怎样判断两个值相等(要考虑都是null的情况)
declare l_test01 varchar2(10); l_test02 varchar2(10); begin if l_test01 is null then dbms_output.put_line('l_test01 is null'); end if; if l_test01 = l_test02 then dbms_output.put_line('l_test01 = l_test02'); end if; if l_test01 is null and l_test02 is null then dbms_output.put_line('l_test01 is null and l_test02 is null'); end if; if l_test01 = l_test02 or (l_test01 is null and l_test02 is null) then dbms_output.put_line('l_test01 = l_test02 '); end if; end; --output --l_test01 is null --l_test01 is null and l_test02 is null --l_test01 = l_test02
跟踪store procedure or function执行情况
--通过看c_execute_num计数器,查看执行情况,也可以在块中打印来查看执行情况 declare v_para varchar2(20); c_count number; c_execute_num number := 0; begin select cd into v_para from t_mt_ctry; c_execute_num := c_execute_num+1; c_count := c_count/0; c_execute_num := c_execute_num+1; select cd into v_para from t_mt_ctry where cd='XX'; c_execute_num := c_execute_num+1; insert into t_mt_ctry(id) values(null); c_execute_num := c_execute_num+1; exception when others then dbms_output.put_line('c_execute_num ' || c_execute_num); end; -- 另一种方法是将每一条语句都放置在它自己的子块中,这个就比较繁琐 declare v_para varchar2(20); c_count number; begin begin select cd into v_para from t_mt_ctry; exception when others then dbms_output.put_line('select * into v_para from t_mt_ctry ' || 'occur exception '); dbms_output.put_line('sql code is : '||sqlcode || 'sql error message is : '|| sqlerrm); end; begin c_count := c_count/0; exception when others then dbms_output.put_line('c_count := c_count/0; ' || 'occur exception '); dbms_output.put_line('sql code is : '||sqlcode || 'sql error message is : '|| sqlerrm); end; begin select cd into v_para from t_mt_ctry where cd='XX'; exception when others then dbms_output.put_line('select cd into v_para from t_mt_ctry where cd='XX'; ' || 'occur exception '); dbms_output.put_line('sql code is : '||sqlcode || 'sql error message is : '|| sqlerrm); end; begin insert into t_mt_ctry(id) values(null); exception when others then dbms_output.put_line('insert into t_mt_ctry(id) values(null)' || 'occur exception '); dbms_output.put_line('sql code is : '||sqlcode || 'sql error message is : '|| sqlerrm); end; end;
ORACLE异常分为两种类型:系统异常、自定义异常。其中系统异常又分为:预定义异常和非预定义异常。
常见预定义异常如下:
错误号 | 异常错误信息名称 | 说明 |
ORA-0001 | Dup_val_on_index | 违反了唯一性限制 |
ORA-0051 | Timeout-on-resource | 在等待资源时发生超时 |
ORA-0061 | Transaction-backed-out | 由于发生死锁事务被撤消 |
ORA-1001 | Invalid-CURSOR | 试图使用一个无效的游标 |
ORA-1012 | Not-logged-on | 没有连接到ORACLE |
ORA-1017 | Login-denied | 无效的用户名/口令 |
ORA-1403 | No_data_found | SELECT INTO没有找到数据 |
ORA-1422 | Too_many_rows | SELECT INTO 返回多行 |
ORA-1476 | Zero-divide | 试图被零除 |
ORA-1722 | Invalid-NUMBER | 转换一个数字失败 |
ORA-6500 | Storage-error | 内存不够引发的内部错误 |
ORA-6501 | Program-error | 内部错误 |
ORA-6502 | Value-error | 转换或截断错误 |
ORA-6504 | Rowtype-mismatch | 宿主游标变量与 PL/SQL变量有不兼容行类型 |
ORA-6511 | CURSOR-already-OPEN | 试图打开一个已处于打开状态的游标 |
ORA-6530 | Access-INTO-null | 试图为null 对象的属性赋值 |
ORA-6531 | Collection-is-null | 试图将Exists 以外的集合( collection)方法应用于一个null pl/sql 表上或varray上 |
ORA-6532 | Subscript-outside-limit | 对嵌套或varray索引得引用超出声明范围以外 |
ORA-6533 | Subscript-beyond-count | 对嵌套或varray 索引得引用大于集合中元素的个数. |
你可以将一个经过命名的异常和一个特别的ORACLE错误相关联。这会使你专门能够捕获此错误,而不是通过WHEN OTHERS处理器来进行捕获。
EXCEPTION_INIT pragma的语法如下:
PRAGMA EXCEPTION_INIT(exception_name,Oracle_error_number);
这里,exception_name是在PRAGMA前面声明的异常的名字,而Oracle_error_number是与此命名异常相关的所需错误代码。这个PRAGMA必须在声明部分.
RAISE_APPLICATION_ERROR和RAISE的比较
RAISE_APPLICATION_ERROR只能被others捕获,不能用已经命名的异常子程序捕获。而RAISE是可以的。RAISE_APPLICATION_ERROR可以填写自己的错误信息,而RAISE不行。
关于Record的一二事
由单行多列的标量构成的复合结构,可以看做是用户自定义的数据类型,将一个或者多个标量封装到一个对象来操作,也可看做是一种临时复合对象类型。
Record可以直接赋值,但是不可以整体比较和整体判断是否为空。
%rowtype和record区别是:
#1,%rowtype的结构是表结构,而record则可以根据实际情况自定义结构。
#2,在insert或者update中,只可以用record的成员,但是可以直接用%rowtype.
declare Type record_type is record( c_id t_mt_ctry.id%type, c_cd t_mt_ctry.cd%type); c_row t_mt_ctry%rowtype; my_record record_type; begin select id,cd into my_record.c_id,my_record.c_cd from t_mt_ctry where cd='MY'; select * into c_row from t_mt_ctry where cd='SG'; insert into t_mt_ctry(id,cd ) values(my_record.c_id,my_record.c_cd); dbms_output.put_line('c_row ' || c_row.cd); insert into t_mt_ctry values (c_row); end;
关于Cursor的一二事
--没有参数,没有返回值 declare v_ctry_id t_mt_ctry.Id%type; v_ctr_cd t_mt_ctry.cd%type; cursor my_cursor is select id ,cd from t_mt_ctry; begin open my_cursor; loop fetch my_cursor into v_ctry_id,v_ctr_cd; if my_cursor%notfound then dbms_output.put_line('finish read my_cursor '); exit; else dbms_output.put_line('my_cursor ' || 'v_ctry_id : '|| v_ctry_id || 'v_ctr_cd : '||v_ctr_cd); end if; end loop; end; --有参数,没有返回值 declare v_ctry_id t_mt_ctry.Id%type; v_ctr_cd t_mt_ctry.cd%type; cursor my_cursor(para_cd varchar2 default 'SG') is select id ,cd from t_mt_ctry where cd=para_cd ; begin open my_cursor('MY'); loop fetch my_cursor into v_ctry_id,v_ctr_cd; if my_cursor%notfound then dbms_output.put_line('finish read my_cursor '); exit; else dbms_output.put_line('my_cursor ' || 'v_ctry_id : '|| v_ctry_id || 'v_ctr_cd : '||v_ctr_cd); end if; end loop; end; --有参数,有返回值 declare type my_record is record( v_ctry_id t_mt_ctry.Id%type, v_ctr_cd t_mt_ctry.cd%type ); v_my_record my_record; cursor my_cursor(para_cd varchar2 default 'SG') return my_record is select id ,cd from t_mt_ctry where cd=para_cd ; begin open my_cursor('MY'); loop fetch my_cursor into v_my_record; if my_cursor%notfound then dbms_output.put_line('finish read my_cursor '); exit; else dbms_output.put_line('my_cursor ' || 'v_ctry_id : '|| v_my_record.v_ctry_id || 'v_ctr_cd : '||v_my_record.v_ctr_cd); end if; end loop; end;
相关文章推荐
- PL/SQL - 基础知识(1)
- PL/SQL基础知识
- oracle PL/SQL基础知识2
- PL/SQL基础知识讲解之异常(EXCEPTION)
- PL/SQL - 基础知识(2)
- 【PL/SQL基础知识】结构
- PL/SQL(一):基础知识
- PL/SQL 基础知识简介
- pl/sql基础知识学习笔记(三)
- pl/sql基础知识学习笔记(二)
- PL/SQL基础知识
- 第10章 PL/SQL基础知识
- Oracle PL/SQL基础知识
- PL/SQL编程基础知识(一)
- Oracle基础知识(二十七) - PL/SQL修改字符编码
- PL/SQL基础知识讲解之游标 (CURSOR)
- PL/SQL 基础知识
- PL/SQL开发基础知识
- PL/SQL块基础知识
- Oracle基础知识:在PL/SQL中修改ORACLE的字段顺序