p fetch c1 into v_stu; exit when c1%notfound; dbms_output.put_line('name'||v_stu.name||' '||'ss'||v_stu.ss); end loop; close c1; end Cursor1; 2.使用游标for循环来操作游标 create or replace procedure Cursor2 ( v_ss nvarchar2 ) is cursor c1 is select * from stu where ss=v_ss; begin for c1_rec in c1--c1_rec为能存储游标单行的记录变量 loop dbms_output.put_line('name'||c1_rec.name||' '||'ss'||c1_rec.ss); end loop; end Cursor2; 使用游标for循环,自动完成打开关闭游标的功能 3.使用游标来更新和删除数据。 create or replace procedure Cursor3 ( v_ss nvarchar2 ) is cursor c1 is select * from stu where ss=v_ss for update;--加一个for update begin for c1_rec in c1 loop update stu set name='nb' where current of c1; --逐行更新。 end loop; end Cursor3; Oracle触发器介绍 触发器是一种特殊的过程,与普通过程不同的是,过程需要用户显式的调用才能执行,而触发器则是当事件发生时oracle服务器触发执行。 要点: 触发时机:before和after,在动作前触发还是动作后触发 触发事件:insert update delete或是三个的逻辑组合 条件谓词:inserting(insert 触发时为true) updating [column x](更新具体列是为true)deleting(删除时为true) 语法: CREATE [OR REPLACE] TRIGGER trigger_name AFTER | BEFORE | INSTEAD OF [INSERT] [[OR] UPDATE [OF column_list]] [[OR] DELETE] ON table_or_view_name [REFERENCING {OLD [AS] old / NEW [AS] new}] [FOR EACH ROW]—行级触发器,每操作一行都会触发一次 [WHEN (condition)] pl/sql_block; 如: create or replace trigger test1 before update on stud declare begin DBMS_OUTPUT.PUT_LINE('更新了'); if updating('NAME') THEN DBMS_OUTPUT.PUT_LINE('更新了名字'); end if; end test1; 当执行:update stud set sex='男' where ID=3;时会输出“更新了” 当执行update stud set NAME='测试' where ID=3;时会输出“更新了”和“更新了名字” 再如: create or replace trigger test1 before update on stud declare begin raise_application_error(-20001,'不让更新');--该触发器的作用是在更新之前告诉用户不让其更新。 end test1; 在数据字典中查看某个表中的触发器 select * from user_triggers where table_name='STUD'; 禁用触发器 Alter trigger test1 disable 激活触发器 Alter trigger test1 enable 禁用表上所有的触发器 Alter table dept(表名) disable all triggers 启用表上所有的触发器 Alter table dept(表名) enable all triggers 当改变表结构时重新编译触发器 Alter trigger test1 compile; 删除触发器 drop trigger test1; 包: 包类似于java中的类,用于将pl/sql中的函数或者过程以及变量进行封装 包分为两部分 1)包说明:里边放置的包的公有组件,如变量,常量,过程等 创建语法: create or replace package dd is -- Author : RR -- Created : 2011/7/15 20:38:53 -- Purpose : -- Public type declarations type is ; -- Public constant declarations constant := ; -- Public variable declarations ; -- Public function and procedure declarations function ( ) return ; end dd; 2)包体 放置具体的实现代码 删除包 Drop package package_name
|