【管理过程、函数和包】 PL/SQL主要有匿名块和命名块。 匿名块的缺点是在每次执行时都要被编译,不能存储在数据库中供其它PL/SQL块调用。 过程:为了执行一定任务而组合在一起的PL/SQL块,它存储在数据字典中并可被应用程序调用。当执行一个过程时,其语句被作为一个整体执行。过程是没有返回值的。 1.创建一个过程myproc,功能为根据学号检索学生的姓名、性别、年龄和班级等信息。 create or replace procedure system.myproc (v_sno varchar2,v_sname out varchar2,v_ssex out varchar2,v_sage out varchar2,v_sclass out varchar2) is begin select sname,ssex,sage,sclass into v_sname,v_ssex,v_sage,v_sclass from student where sno=v_sno; end myproc; 2.执行execute myproc(); 3.调用 set serveroutput on; declare a student.sname%type; b student.ssex%type; c student.sage%type; d student.sclass%type; begin myproc('02',a,b,c,d); dbms_output.put_line('学号:'||'02'); dbms_output.put_line('姓名:'||a); dbms_output.put_line('年龄:'||b); dbms_output.put_line('性别:'||c); dbms_output.put_line('班级:'||d); end; 4.删除 drop procedure myproc; 【函数】----------------------------------------------------- 1.函数 create or replace function myfunc(v_sno varchar2,v_total out number) return varchar2 is v_avgscore varchar2(4); begin select count(*),avg(grade) into v_total,v_avgscore from score where sno=v_sno group by sno; return v_avgscore; end myfunc; 2.调用 set serveroutput on; declare total number; avgscore score.grade%type; begin avgscore:=myfunc('03',total); dbms_output.put_line('学号:'||'03'); dbms_output.put_line('选修课程门数:'||total); dbms_output.put_line('平均成绩:'||avgscore); end; --------------------------------------------------------------------------- 【包】 1.包括两个部分:规范(包头:声明)与主体 2.规范中的声明是全局的;主体中的声明只对主体部分可见。 3.包的创建分为两个部分:规范的创建和主体的创建。 ==创建一个包,能够将对学生表的查询、插入、删除、修改等操作封装在其中。 create or replace package mypack as procedure myproc1; procedure myproc2(v_sno varchar2,v_sname varchar2,v_ssex varchar2,v_sage varchar2,v_sclass varchar2); procedure myproc3(v_sno varchar2); procedure myproc4(v_sno varchar2,v_sclass varchar2); end mypack; =============================================================== create or replace package body mypack as procedure myproc1 as cursor mycursor is select * from system.student; v_sno system.student.sno%type; v_sname system.student.sname%type; v_ssex system.student.ssex%type; v_sage system.student.sage%type; v_sclass system.student.sclass%type; begin open mycursor; fetch mycursor into v_sno,v_sname,v_ssex,v_sage,v_sclass; while mycursor%found loop dbms_output.put_line(v_sno||' '||v_sname||' '||v_ssex||' '||v_sage||' '||v_sclass); fetch mycursor into v_sno,v_sname,v_ssex,v_sage,v_sclass; end loop; end myproc1; procedure myproc2(v_sno varchar2,v_sname varchar2,v_ssex varchar2,v_sage varchar2,v_sclass varchar2)as begin insert into system.student values(v_sno,v_sname,v_ssex,v_sage,v_sclass); end myproc2; procedure myproc3(v_sno varchar2)as begin delete from system.student where sno=v_sno; end myproc3; procedure myproc4(v_sno varchar2,v_sclass varchar2)as begin update system.student set sclass=v_sclass where sno=v_sno; end myproc4; end mypack; -------------------------------------------------------------- create or replace function chaxun1(v_date varchar2) return number is v_fanhui number; begin select count(*) into v_fanhui from hosp where riqi1=v_date; return v_fanhui; end chaxun1; create or replace function chaxun2(v_date varchar2) return number is v_fanhui number; begin select count(*) into v_fanhui from hosp where riqi2=v_date; return v_fanhui; end chaxun2; set serveroutput on; declare jieguo1 number; jieguo2 number; begin jieguo1:=chaxun1('2003-1-1'); jieguo2:=chaxun2('2003-1-1'); dbms_output.put_line('住院人数'||jieguo1); dbms_output.put_line('出院人数'||jieguo2); end;