--存储过程的创建
使用存储过程来为student表插入一条数据
create or replace procedure pro_addstudent
(sname in student.sname%type , sage in student.sage%type,sgender in student.sgender%type)
is
begin
insert into student values(seq_student.nextval , sname , sage, sgender);
end;
注释:sname为输入参数使用关键字in标识,类型为 student表中sname字段的类型,sage同理
--存储过程的使用
begin
pro_addstudent('汉汉',23,'nan');
end;
--存储过程的创建二
使用存储过程实现在为student删除一条记录是,先返回要删除的学生的姓名,如果存在删除成功,如果不存在,抛出异常,显示未找到数据
create or replace procedure pro_delstudent
(s in student.sid%type,sn out student.sname%type)
is
begin 将查询到的sname值赋给输出参数sn
select sname into sn from student where sid=s;
delete from student where sid=s;
--异常处理
exception 未查询相关的sname数据时抛出异常
when no_data_found then sn:='未找到数据';
end;
注释: s为输入参数,sn为输出参数使用out关键字标识,类型为student表中sname的类型
--存储过程的使用二
declare sname student.sname%type;
--声明一个sname的变量,来保存存储过程输出的结果
begin
pro_delstudent(8,sname); 调用存储过程
dbms_output.put_line(sname); 打印存储过程输出的结果
end;
Java中使用存储过程的
//存储过程的调用
CallableStatement callableStatement=conn.prepareCall("{call pro_delstudent(?,?)}"); //?站位,表示参数
callableStatement.setInt(1,2); //注册参数
callableStatement.registerOutParameter(2,java.sql.Types.VARCHAR)
callableStatement.execute(); //执行
String str=callableStatement.getString(2); //获取返回参数
System.out.println(str); //答应结果
二.自定义函数的关键字: function
--例一 输出拼接课程表course的id
create or replace function fun_str 无参数的
return varchar2 指明返回类型返回值return 显示返回
is str varchar2(50); is 后声明了一个str变量
begin
for i in (select cid from course) loop
str:=str||i.cid||','; //使用 || 对字符进行拼接
end loop;
return str; //返回str
end;
--自定义函数的使用,直接调用即可
begin
dbms_output.put_line(fun_str);
end;
java中使用自定义函数
CallableStatement fun_call=conn.prepareCall("{?=call fun_str}");
fun_call.registerOutParameter(1,java.sql.Types.VARCHAR);
fun_call.execute();
String funstr=fun_call.getString(1);
System.out.println(funstr);
--例二转换输出日期数据
带参数的自定义函数也使用in关键字表识,但是没有out关键字
create or replace function fun_date(td in date)
return varchar2
is
str varchar2(50); 声明变量 str
begin 为str赋值
select to_char(td,'yyyy"年"mm"月"dd"日" hh24:mi:ss') into str from dual;
return str;
end;
--调用日期
select cid,cname,tid,fun_date(ctime) as 日期 from course;
三.触发器:关键字 trigger
--触发器,为课程表course添加数据触发器
create or replace trigger trig_course_add
before insert [or update , delete ] //表示在插入之前执行
on course on 后接的是表明,置顶触发的表
for each row 每一行改变时都执行触发器
begin
--:new.cid 表示新的cid的值将序列的自增值赋给新的cid
select seq_course.nextval into :new.cid from dual;
end;
--在重新插入的时候就不需要在values中指定序列了
insert into course (cname,tid) values('体育',3);
触发器都是被隐式调用
--创建触发器,为课程表添加增、删、改操作时记录到日志表的功能
createorreplacetrigger trig_course_log
afterinsertorupdateordelete
on course
foreachrow dblog为日志表
declare str1 dblog.logmsg%type; str2 g.logmsg%type;
begin
if inserting then 如果是插入
select :new.cname into str1 from dual;
str1:='添加了课程:'||str1;
elsif updating then 如果是修改
select :old.cname into str1 from dual;
select :new.cname into str2 from dual;
str1:=str1||':课程改为:'||str2;
-- dbms_output.put_line(str1);
elsif deleting then 如果是删除
select :old.cname into str1 from dual;
str1:='删除了课程:'||str1;
endif;
insertinto dblog values (seq_dblog.nextval,'course',str1); 插入日志信息
end;