公司动态
plsql触发器异常包
异常1. 预定义异常2. 非预定义异常3. 自定义异常1. 预定义异常 是指 oracle 已经设定好的异常 包含 异常名 异常信息 异常代码。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。CASE_NOT_FOUND ----异常名使用CASE语句时在WHEN子句中没有包含必需的条件分支并且没有包含ELSE语句。对应ORA-06592错误。---异常信息-06592 ------异常代码COLLECTION_IS_NULL给集合元素赋值前必需初始化集合元素。对应ORA-06531错误。CURSOR_ALRADY_OPEN重新打开已经打开的游标。对应ORA-06511错误。DUP_VAL_ON_INDEX在惟一索引所对应的列上键入重复值。对应ORA-00001错误。INVALID_CURSOR试图操作不合法的游标。对应ORA-01001错误。INVALID_NUMBER内嵌SQL语句不能有效的将字符转换成数字。对应ORA-01722错误。NO_DATA_FOUND执行SELECT INTO未返回行。对应ORA-01403错误。TOO_MANY_ROWS执行SELECT INTO语句时返回超过一行。对应ORA-01422错误。ZERO_DIVIDEPL/SQL块中使用数字除0对应ORA-01476错误。SUBscript__BEYOND_COUNT元素下标超出嵌套表或VARRAY元素的范围。对应ORA-06533错误。SUBscript__OUTSIDE_LIMIT使用嵌套表或VARRAY元素时元素下标为负。对应ORA-06532错误。VALUE_ERRORPL/SQL中赋值操作时变量长度不足以容纳实际数据,或尝试将无效的字符串转换成数据。对应ORA-06502错误。LOGIN_DENIED连接到ORACLE数据库时用户名/密码不正确。对应ORA-01017错误。NOT_LOGGED_ON应用程序没有连接到数据库。对应ORA-01012错误。PROGRAM_ERRORPL/SQL内部问题。对应ORA-06510错误。ROWTYPE_MISMATCH执行赋值操作时宿主游标变量和PL/SQL游标变量返回类型不兼容。对应ORA-06504错误。SELF_IS_NULL在NULL实例上调用成员方法。对应ORA-30625错误。STORAGE_ERRORPL/SQL块运行时超出了内存空间或者内存被破坏。SYS_INVALID_ROWID将字符串转变为ROWID时没有使用有效的字符串。对应ORA-01410错误。TIMEOUT_ON_RESOURCEORACLE在等待资源时超时。对应ORA-00051错误。TRANSACTION_BACKED_OUT由于死锁提交被退回 对应ORA-006 错误。---捕获异常语法declarebeginexception -----只能捕获异常 不能修改异常when 异常名 then 异常代码when 异常名 then 异常代码when 异常名 then 异常代码when 异常名 then 异常代码when 异常名 then 异常代码when 异常名 then 异常代码..................................................end;declarebegindbms_output.put_line(1/0);exceptionwhen ZERO_DIVIDE thendbms_output.put_line(除数为0);end;未知异常: OTHERS错误信息: SQLERRM错误代码: SQLCODEdeclarebegindbms_output.put_line(1/0);exceptionwhen OTHERS thendbms_output.put_line(SQLERRM|| ||SQLCODE );end;--------------------------------------------------------------------非预定义异常 : 没有名字 只有异常信息和异常代码语法declare异常名 exception ; -----声明一个异常名PRAGMA EXCEPTION_INIT(异常名,异常代码) ;--第二步 将异常的名字和异常代码进行绑定beginexceptionwhen 异常名 then异常代码........................end;-------------------------------自定义异常 --没有名字 没有代码 没有信息 完全自定义语法declare异常名 exception ; -----声明一个异常名PRAGMA EXCEPTION_INIT(异常名,自定义代码) ; ---2 绑定代码 -20001 ~ - 20999beginraise_application_error(自定义代码,自定义异常信息);---手动抛出异常--自定义异常需要手动抛出exceptionwhen 异常名 then异常代码........................end;declarebeginraise_application_error(-20001,付浩浩); ----手动抛出异常end;------------------------------------------------------------------------------------------------------------------------------------触发器 trigger定义 是 由 DML 语句 影响的 触发事件创建触发器语法create [or replace] trigger 触发器名before|after ------之前 之后update or insert or delete ------用什么写什么on 表名[for each row ] ----------------行级触发器begin-----触发的事件 事情 内容end;行级触发器 -------是指 DML 语句 影响了多少行数据 触发器 就会触发多少次delete from emp表级触发器 ------表级 是指 无论 DML 语句影响了多少行数据 触发器 只会触发一次例题 创建一个触发器 对 emp表进行增 删 改 则会触发触发器触发事件 是打印输出 修改了数据create or replace trigger t_97beforeupdate or delete or inserton empbegindbms_output.put_line(修改了数据);----触发事件end;insert into emp(empno,ename) values(6789,晁洪涛)update emp set sal1delete from empcreate or replace trigger t_97beforeupdate or delete or inserton empfor each rowbegindbms_output.put_line(修改了数据);----触发事件end;例题 创建一个触发器 对 emp表进行增 删 改 则会触发触发器触发事件 是插入数据的时候 打印输出 插入了数据修改数据的时候 打印输出 修改了数据删除数据的时候 打印输出 删除了数据触发器的三个判断属性deleting --判断当前操作是否为 delete 如果是则返回 trueupdating --判断当前操作是否为 update 如果是则返回 trueinserting --判断当前操作是否为 insert 如果是则返回 truecreate or replace trigger t_97beforeupdate or insert or deleteon empfor each rowbeginif deleting thendbms_output.put_line(删除了数据);elsif updating thendbms_output.put_line(修改了数据);elsif inserting thendbms_output.put_line(增加了数据);end if;end;insert into emp(empno,ename) values(6789,晁洪涛)update emp set sal1delete from empselect * from emp----------------------------------------------------------行级触发器 的两个属性:old ----------------------修改前的数据:new ----------------------修改后的数据例题 创建一个触发器 修改emp表的工资 如果 修改前的工资小于修改后的薪资则打印输出涨薪 否则 打印输出降薪create or replace trigger t_97beforeupdateon empfor each rowbeginif :old.sal :new.sal thendbms_output.put_line(涨薪);elsif :old.sal:new.sal thendbms_output.put_line(降薪);elsedbms_output.put_line(不变);end if;end;update emp set sal3000-----------------------------------------------------------包 -------------一系列函数 和存储过程的集合-----------------方便迁移 不容易乱包头 package ---------标签 标明了 包里面的内容包体 package body ---装东西包头 package创建语法create [or replace] package 包名is----声明 要装的存储过程 和函数end;create or replace package 爱马仕isfunction fu_97(p_max number) return number ; ---声明 fu_97function fu_96(V_deptno number) return number; ----fu_96procedure sp_97(p_deptno in number,o_emp_count out number,o_avg_sal out number);--sp_97end ;创建包体语法create [or replace ] package body 包体名 ---和包头一致is-------------装end;create package body 爱马仕 ---和包头一致is-------------装function fu_97(p_max number)return numberisn number:0;beginfor i in 1..p_max loopif mod(i,2)0 thenn: n i;end if;end loop;return n;end;---fu_97function fu_96(V_deptno number)return numberisV_avg number;beginselect avg(sal) into V_avg from emp where deptnoV_deptno;return V_avg;end;--fu_96procedure sp_97(p_deptno in number,o_emp_count out number,o_avg_sal out number)isbeginselect avg(sal),count(1) into o_avg_sal,o_emp_count from emp where deptnop_deptno;if o_emp_count5 theno_avg_sal: o_avg_sal*1.1;end if;end; ---sp_97end;select fu_96(10) from dualselect 爱马仕.fu_96(10) from dualdbms_output.put_line( 1 )-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------数据库的基础 sql plsql 结束