oracle中的procedure,function,trigger
上一篇 / 下一篇 2007-12-13 12:35:03 / 个人分类:Oracle数据库

1. procedure
--修改student表中对应id的学生姓名
D$Gg)IK~117123create or replace procedure p_modifyNameITPUB个人空间
XC+_+|sVX@6]
(v_Name in varchar2,v _Id char) as --and another:out,in outITPUB个人空间ct
a3DC#kD^x
begin
qv\AE8R"|117123update student set name=v_name where id=v_Id;
X b0{'V7NL117123commit;ITPUB个人空间OT Z+w~D
end p_modifyName;
#o+j/s6Jn1L117123--调用存储过程ITPUB个人空间n[,Zs7@_ng)F!C:y)u
declare
v_Name varchar2(10);ITPUB个人空间w["ee#TVZ|\/X\
v_Id char(10);ITPUB个人空间j5Y
Z [ AT5G sh/m
beginITPUB个人空间&\,P
r'H$c`u
v_Name:='朱林';ITPUB个人空间?Zyrz
v_Id:='0420622';
--调用上面创建的存储过程
#Kk ^(]`|7_v0]117123p_modifyName(v_Name,v_Id);
end;
--p_modifyName('朱林','420622');
--位置标志法:要按照位置排序ITPUB个人空间t;Pi)IJ~CB
--带名标志法:
SYJCM t)D117123declareITPUB个人空间,A,]0H,a1cKP
v_Sname varchar2(10);
5K9~Tl \?!Ui'l117123v_Sid char(10);
5b*VR[sfSAR7e117123begin
0ZP$c o8t9jkr,~v117123v_Sname:='朱林';ITPUB个人空间)p/kT
[ E G.Z"Xg
v_Sid:='0420622';
p_modifyName(v_Id=>v_Sid,v_Name=>v_Sname);
--删除存储过程ITPUB个人空间4L~ED$[f}4H1Z
drop procedure P_modifyName;
--增加权限ITPUB个人空间;c1~^i|
grant execute on ProceduceName to UserName;
r9kHQx5l*C%se s117123revoke execute on ProceduceName from UserName;
实例
y(qFS!n D
F117123create or replace procedure for_loop(ITPUB个人空间RdK4r*]
v_num1 in number;ITPUB个人空间;^(ErOBx
p*~)b_}
v_num2 in number;)
OXN)r[ ]117123isITPUB个人空间0?E:nmXW:a
i number(3);
8c1UA@9U
}117123begin
-R_Xy:l Ktehn_117123 if v_num1<=v_num2 thenITPUB个人空间sz
v?sP
i:=v_num1;ITPUB个人空间8yW;^4yh
for i in v_num1..v_num2 loop --这里的循环是循环到v_num2的值就结束循环了ITPUB个人空间k+[ i+o|H%eo
dbms_output.putline(i);
!L\\8il0EwIeojQ(Q117123 end loop;ITPUB个人空间Ow D UXo
end if;ITPUB个人空间K!V!WF+um
end;ITPUB个人空间jo@\!R
查看所写的存储过程ITPUB个人空间,M i|!V/] Z\
select *from dba_source where type='procedure';
2.function
--函数功能:ITPUB个人空间*\KMJ;j
--得到指定系,指定课程的出勤情况,如果全部出勤,则返回'full',如果出勤超过80%,则返回'Some Room',超过60%,则返回'More Room',如果出勤小余60%,返回'Lots of room',ITPUB个人空间Jvp[E|H6q
--没有出勤,返回'Empty'ITPUB个人空间g t)f5s@#Gazz
create or replace function GetClassInfo(
\vg$W!TCA117123--系名
+h3u$o
r7p s5b
?@117123v_Dept classes.department%type,ITPUB个人空间!_0giU'w9wj
`1x
--%type:v_Dept 继承了classes表中department字段的数据类型和精度ITPUB个人空间+bx/\VWf
--课程名ITPUB个人空间#]Kbv,jWG;z
v_Course class.course%type)
_*l(HX&ES*W-Q_Jw0y117123return varchar2 is
4NhuA5TX(V`117123--出勤的学生数ITPUB个人空间D_igu$s&z
n_CurrSNum number;
(Rdj-eP+S:ML{117123--学生人数ITPUB个人空间2vB:ipSW#Q
n_MaxSNum number;
6IA2Nco117123--出勤率ITPUB个人空间h JY9Aq%WZ
n_PercentFull number;ITPUB个人空间G#_ x*b:{qJ r
begin
#w;W[hW
HS117123--得到学生的出勤人数和学生总人数ITPUB个人空间n3Wr"n L-j
select current_students,max_studentsITPUB个人空间2L6zeRqH/G#QY
--查询的返回值被赋予INTO子句中的变量
8M#J@2A;s:G8V117123 into n_CurSNum,n_MaxSnumITPUB个人空间cH*ix#{2X8b
from classes
|(R0y#e o117123 where department=v_DeptITPUB个人空间1R#Q3f*P^,k!El1qB
and course=v_Course;ITPUB个人空间N _!q7N-dOj
--计算出勤率
n_PercentFull:=n_CurSum/n_MaxSum*100;ITPUB个人空间 YNha{"L
C
--返回值
t+wttHR/@~&{117123if n_PercentFull=100 then
E6W~6cgEo8y1b117123return 'Full';
TW TI8G/G117123....ITPUB个人空间}&u+aU%X#W
j7c
...ITPUB个人空间_t
]!j9O7c^8i`{ O
end if;ITPUB个人空间B|.KXR${N4C6Y
end GetClassInfo;
j`C O6[6I Md8U7?117123--调用函数ITPUB个人空间XFG5AW._pm*[
declare
^"C;G7Gl'_2^
w5z117123v_Dept varch2(30);
+W]G!o)qo8h117123v_Course varchar2(30);
w.W^eM2@6U5K+O117123v_ClassInfo varchar2(20);
aL l
k*[?117123begin
z&`9J[@g0f117123v_Dept:='水利系';
9D;U``p-N117123v_Course varchar2:='水力学';
"ErHqGLB)Rz@117123v_ClassInfo:=GetClassInfo(v_Dept,v_Course);ITPUB个人空间z U"VJ"v
dbms_output.put_line(v_ClassInfo);ITPUB个人空间8pW-p*n3fI+X'R2o
end;
3.trigger
--indert,delete,update触发t_UpdateMajorStats,保持major_stats最新记录
3O9g3EKs;w YGc117123--create major_stats
YN+Dgun&iK-_6O117123create table major_stats(
a
OU.b%S117123major varchar2(30),
PX;J$O6D V7De117123total_credits number(3),ITPUB个人空间)eg4Q~6g2veiZ6G
total_students number(4);
-- create trigger major_stats,update major_stats
tb
C+n%j117123create or replace trigger t_UpdateMajorStats
after insert or delete or update on student
0iw2T$E.OM+|]T117123declare
5j!]*bE:^X117123cursor c_Statistics is --define a cursorITPUB个人空间6u:i4GgC {7d7~
select major,count(*) as total_students, --count(name) etc
"`#C)k3g+bQ3u[117123sum(current_credits) as total_credits
F`aL{
r.N117123from student --get new record from student
group by major;ITPUB个人空间Y$Z,G:?HA5l!q
beginITPUB个人空间{.[1`)q&}d-E
delete from major_stats; --clear major_stats
UeR+HZQq+S117123for v_StatsRecord in c_Statistics loop --
.D O$HHk2Dmn117123--for 循环变量 in [reverse] 初始值..结束值 loop
`)z@"}s1Dn`117123insert into major_stats(major,total_credits,total_students)
)IL:VIK'uy117123values (v_StatsRecord.major,v_StatsRecord.total_credits,
7[,k@3[n'u(X117123v_StatsRecord.total_students); --insert new record into major_stats
*AB;FkitiY3p117123end loop
?Q:J-N-s117123end t_UpdateMajorStats;
E-W
Z!{!b$eYEq117123/
--"show errors;" can show error from trigger
ITPUB个人空间Mcc;f;`
--another simple example
e
hxJCQ|117123create or replace trigger T_DEL_EMP
rn0{M1xd117123before delete on emp
,hWs/yJu.LI.p
H117123for each row
/x4DBW8V${+g4[117123insert into del_emp(depno,empno,ename)
Y,@#UVHy+Cb1_9A117123values (:old.depon,:old.empno,:old.ename);
&D7im.ff117123end;
? {K)t*y8R6r3j_0bf117123 ITPUB个人空间
]4w4@~T6kR
--delete the trigger forerver
6G'qlG4A x_117123drop trigger trigger_name;ITPUB个人空间Ca8KPkB2}(h
--undo triggerITPUB个人空间Xh{)aly3IA
alter trigger trigger_name disable;ITPUB个人空间[!h
X W Iiw
alter trigger trigger_name enable;
--格式化代码
*每开始一个新的代码块时,应该缩进2-5个空格,注意再每个这样的代码结束后,应该取消这个缩进ITPUB个人空间}ow
jIf2x
W
*关键字用大写,这样可以区分常规代码和oracle提供的代码ITPUB个人空间J8X6v"R*o|ci
*变量名采用大小写混合模式ITPUB个人空间1x)KaNJ J}s
*每条语句用一行,增强程序的可读性ITPUB个人空间"}7oQV Jr
T#q_|cD117123刚接触oracle,希望和我一样的朋友共同进步!!
相关阅读:
- 学习笔记一:oracle体系结构组件 (focus.world, 2007-12-20)
- Oracle数据库的物理结构简介 (chenqilong, 2007-12-20)
- 监控当前数据库用户在运行什么sql语句 (ysfabm, 2007-12-22)
- oracle 学习之路 (转载) (sj04620118, 2007-12-31)
- Oracle 的入门心得(转载) (sj04620118, 2007-12-31)
- 逻辑数据库结构概述 (chenqilong, 2008-1-03)
- Oracle是怎样工作的 (chenqilong, 2008-1-04)
- 使用Oracle可传输表空间的特性复制数据(1)基础概念 (junsansi, 2008-1-08)
- [原创]Oracle ERP展开BOM结构树的SQL (ysfabm, 2008-1-15)
- 关于merge into 的使用 (小鸡飞过海, 2008-1-18)
导入论坛 引用链接 收藏 分享给好友 推荐到圈子 管理 举报
TAG: oracle

