|
/* 创建序列 */
--1.用户序列 create sequence user_seq increment by 1 minvalue 1 start with 1 maxvalue 9999; --初始值从1开始,每次跳跃区间值为1,最大值为9999
/* 触发器 */
--1.1.添加用户时的触发器 create or replace trigger add_user_tri before insert on t_user for each row begin select user_seq.nextval into :new.u_id from dual; end; --user_seq 为建立在数据中的序列sequence的名称 说明:在并发系数不大的时候可以使用触发器来解决oracle表主键的添加问题,每次在有数据插入动作时触发,自动加入主键的值,免去了手动调用sequence的麻烦。但是在高并发的系统中不建议使用触发器。
/* 游标:用一个简单示例来说明
*/ create or replace function test_cur(parm number) return varchar2 as res varchar2(20);
tmp varchar2(30);
cursor cur_user is select username from sys_user; begin if parm is null then
res := ''; else --****************开启游标****************** open cur_user; --循环取游标值对比入参 loop fetch cur_user into tmp; --将游标值赋值给tmp字段 exit when cur_user%notfound; --当游标走完后退出循环 if ... then ... elsif ... then ...
else ...
end if; end loop; close cur_user ; --================关闭游标================= res := tmp; end if; return res; exception when others then begin res := ''; return res; end; end;
/* 存储过程 */
--1.添加用户 create or replace procedure add_user_proc ( uName in t_user.uname%type, uPass in t_user.upass%type, uEmail in t_user.email%type ) is
--变量声明 v_err varchar(500) begin insert into t_user(uName,uPass,email) values(uName,uPass,uEmail); commit; exception when others then v_err:=sqlerrm; --此处为异常处理模块,可记录相应异常信息,sql执行异常信息记录在系统变量“sqlerrm”中,但是要获取里面的信息需要指定给声明的变量,例如:上面声明了变量v_err,这里将异常赋值给它。 rollback; end; --uName ,uPass ,uEmail 三个参数是需要外面传入的参数,数据类型这里设置的为相应数据表“t_user”对应字段“uname”的数据类型,也可以手动指定,例如:varchar2(32);
/* 函数方法 */
CREATE OR REPLACE FUNCTION to_bignum(v_num number) return VARCHAR2 as res VARCHAR2(50); begin if v_num is null then res := '00'; else if v_num < 10 then res := '0' || to_char(v_num); else res := to_char(v_num); end if; end if; RETURN res; end; --此函数用来处理日期格式字符,例如:6月20日,需要指定的格式为06月20日,这里就要将6变为06
|