CREATEPROCEDURE TRANSFER (inaccount INT, outaccount INT, amount FLOAT); DECLARE totaldeposit FLOAT; -- 申明变量 SELECT total INFO totaldeposit FLOAT; SELECT total INTO totaldeposit FROM ACCOUNT WHERE ACCOUNTNUM = outaccount; -- 检查转出余额 IF otaldeposit ISNULLTHEN-- 账户没有存款,回滚事务 ROLLBACK; RETURN; ELSEIF otaldeposit <amount THEN-- 余额不足,回滚事务,并返回 ROLLBACK; RETURN; ELSE--修改转出账户,减去转出额 UPDATE ACCOUNT SET total = total - amount WHERE ACCOUNTNUM = outaccount; UPDATE ACCOUNT SET tatal = total + amount WHERE ACCOUNTNUM = inaccountl COMMIT; -- 转账完成,提交转账事务 ENDIF;
SQL触发器
概述:触发器是用户定义在表上的一类特殊储存过程,触发器的执行是通过事件来出发执行的。
创建触发器
SQL使用CREATE TRIGGER语句创建触发器
1 2 3 4
CREATETRIGGER trigger_name | BEFORE | AFTER | trigger_event ON table_name FOREACH|ROW| STATEMENT | [WHEN<condition>] <trigger_action>
CREATETRIGGER tri_name BEFORE INSERTORDELETEORUPDATEON table_name DECLARE weekend_error EXCEPTION; BEGIN IF TO_CHAR(SysDate, 'DY') ='SAT'OR TO_CHAR(SysDAte, 'DY') ='SUM' THEN RAISE weekend_error; end if EXCEPTION WHEN weekend_error THEN RAISE_APPLICATION_ERROR(-20001, 'error!'); RETURN; END;