




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.:.;第五章 游标和触发器游标:隐式游标:%FOUND, %NOTFOUND ,%ROWCOUNT1%FOUND 用法,只需在DML 语句影响一行或者多行时,%FOUND 属性才前往 TRUE。以下例如演示了 %FOUND 的用法:begin update employees2 set first_name = first_name | t where employee_id = 2;if SQL%found then dbms_output.put_line(数据曾经更新); - dbms_output.put_line(rowCount = |mrowcount);else dbms_o
2、utput.put_line(数据没有找到);end if;end;/以下代码演示了创建了一个游标,前往employees2 表中 salary 大于300000 的记录,留意type 的运用: declare csalary employees2.salary%type; cursor emp2_cursor is select salary from employees2 where salary 300000;begin open emp2_cursor ; loop fetch emp2_cursor into csalary; exit when emp2_cursor%notfou
3、nd; dbms_output.put_line(csalary = |csalary); end loop;end;/以下代码演示了创建了一个游标,前往employees2 表中 division_id=SAL 的记录。留意rowtype 的运用:declare cursor employee2_cursor is select * from employees2 where division_id=SAL; myrecord employees2%rowtype;begin open employee2_cursor; fetch employee2_cursor into myrecor
4、d; while employee2_cursor%found loop dbms_output.put_line(employee id =|myrecord.employee_id); dbms_output.put_line(first Name =|myrecord.first_name); dbms_output.put_line(last name =|myrecord.last_name); fetch employee2_cursor into myrecord;end loop;end;/以下代码演示了带参数的游标,根据division id 查询指定的记录: declare
5、 myrecord employees2%rowtype; cursor emp_cursor(divisionid varchar2) is select * from employees2 where division_id =divisionid;begin open emp_cursor(&divisionid);-loop fetch emp_cursor into myrecord; while emp_cursor%found loop - exit when emp_cursor%notfound; dbms_output.put_line(employee id = |myr
6、ecord.employee_id); dbms_output.put_line(division id = |myrecord.division_id); dbms_output.put_line(first name = |myrecord.first_name); fetch emp_cursor into myrecord;end loop;close emp_cursor;end;/以下代码演示了如何更新 employees2 表中的 first_name 字段:set serveroutput on declare firstName varchar2(20); cursor em
7、ployees2_cursor is select first_name from employees2 where employee_id=1 for update of first_name; begin open employees2_cursor; loop fetch employees2_cursor into firstName; exit when employees2_cursor%notfound; update employees2 set first_Name=jeff where current of employees2_cursor; end loop; clos
8、e employees2_cursor; commit; end; /触发器:触发器是当特定事件出现时自动执行的存储过程特定事件可以是执行更新的DML语句和DDL语句触发器不能被显式调用触发器的功能:自动生成数据自定义复杂的平安权限提供审计和日志记录启用复杂的业务逻辑创建触发器语法:CREATE OR REPLACE TRIGGER trigger_nameAFTER | BEFORE | INSTEAD OFINSERT OR UPDATE OF column_list OR DELETEON table_or_view_nameREFERENCING OLD AS old / NEW AS
9、 newFOR EACH ROWWHEN (condition)pl/sql_block;创建触发器,以下代码演示了插入或者修正 employees2 表中的first_name 假设等于 scott时触发器就会执行:create or replace trigger tri_employees2 before insert or update of first_name on employees2 referencing NEW as newdata OLD as olddata for each row when (newdata.first_name=scott) begin :newd
10、ata.salary :=20000; dbms_output.put_line(new.salary: | :newdata.salary); dbms_output.put_line(old.salary: | :olddata.salary); end;执行以上触发器:insert into employees2 values(38,SUP,WOR,scott,mp,50000);或者:update employees2 set salary=90000,first_name=scott where employee_id=38;以下代码针对数据完好性进展操作: 删除操作: create
11、 or replace trigger del_deptid after delete on dept for each row begin delete from employee where deptid = :old.id; end del_deptid; /执行以上触发器: delete from dept where id=1; 查看employee 表中的 deptid 记录;添加操作: create or replace trigger insert_deptafter insert on deptfor each rowbegin insert into employee(id
12、,name,deptid) values(6,chenmp,:new.id);end;/ 执行以上触发器:insert into dept values(6,销售部门); 查看employee 表中的 deptid 记录修正操作: create or replace trigger update_deptafter update on deptfor each row begin update employee set deptid = :new.id where deptid = :old.id;end;/执行以上触发器:update dept set id=8 where id=1;查看e
13、mployee 表中的 deptid 记录以下代码演示了行级触发器:创建表:drop table rowtable; create table rowtable (id number(8) , name varchar2(100);创建序列 create sequence rowtablesequence;创建触发器:create or replace trigger set_sequencebefore insert on rowtablefor each rowdeclare rsequence number(8);beginselect rowtablesequence.nextval
14、into rsequence from dual; :NEW.id :=rsequence;end;/执行SQL语句: insert into rowtable values(232,scott);以下代码演示了语句级触发器:创建表:create table mylog(curr_user varchar2(100),curr_date date,opera varchar2(10);创建触发create or replace trigger tri_mylogafter insert or delete or update on employees2beginif inserting the
15、ninsert into mylog values(user,sysdate,insert);elsif deleting theninsert into mylog values(user,sysdate,delete);elseinsert into mylog values(user,sysdate,update);end if;end;/INSTEAD OF 触发器 INSTEAD OF 触发器是在视图上而不是在表上定义的触发器,它是用来交换所运用实践语句的触发器。 以下代码创建了视图:create view employee_job asselect e.job_id,e.emplo
16、yee_id,e.first_name,e.last_name, from employees2 e,jobs j where e.job_id = j.job_id;以下代码创建 INSTEAD OF 触发器。create or replace trigger tri_viewinstead of insert on employee_jobfor each rowbegin insert into jobs values(:new.job_id,:); insert into employees2(employee_id,first_name,last_name
17、,job_id) values(:new.employee_id,:new.first_name,:new.last_name,:new.job_id);end;/执行以下语句查看操作: insert into employee_job values(OTH,43,abc,dd,OTHER);方式触发器:可以在方式级的操作上建立触发器,如:create ,alter,drop,grant,revoke 和truncate 等 DDL语句:以下例如对用户所删除的一切对象进展日志记录。创建数据库表: drop table dropped_obj; CREATE TABLE dropped_obj(
18、 obj_name VARCHAR2(30), obj_type VARCHAR2(20), drop_date DATE); 2创建触发器:CREATE OR REPLACE TRIGGER log_drop_objAFTER DROP ON SCHEMABEGIN INSERT INTO dropped_obj VALUES (ORA_DICT_OBJ_NAME, ORA_DICT_OBJ_TYPE, SYSDATE);END;/ 3创建和删除对象: 创建对象:CREATE TABLE for_drop ( x CHAR ); 删除对象:DROP TABLE for_drop;4查看日志表中的信息: SELECT * FROM dropped_obj;起用和禁用触发器: 以下代码演示了
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 演出经纪人资格证必考知识与试题及答案
- 营养师法律知识考试试题及答案
- 2025年房地产经纪人职业道德试题及答案
- 营养师与保安证考试的对比试题及答案
- 专业经纪服务必考试题及答案
- 演出经纪人资格证复习建议试题及答案
- 2025年房地产经纪资格体系完善的试题及答案
- 备战2024营养师证考试试题及答案
- 房地产经纪人考试知识深化技巧试题及答案
- 西藏高考常考题目及答案
- 钢丝绳吊装时最大允许吊装重物对应表
- 梨状窝囊肿的护理查房
- 《做阳光少年主题班会》课件
- 小学中年级数学戏剧剧本小熊卖鱼
- 《有为神农之言者许行》讲读课件
- 樱桃课件完整
- 设计报价单模板
- 幼儿行为观察与分析案例教程第2版全套教学课件
- 医院会计制度科目表
- 校本研修教师手册电子模板
- 应急队伍装备参考目录和急性传染病预防控制技术资料清单
评论
0/150
提交评论