OraclePL SQL函式和觸發器

2021-06-07 11:22:15 字數 2178 閱讀 5931

--建立函式
create

or replace function add_sal(ssal number)

return number
is
begin
if (ssal > 5000) then
return ssal + 51;
elsif (ssal > 3000) then
return ssal + 111;
else
return ssal + 222;
end

if;

end;
select sal, add_sal(sal) from emp;
--觸發器
create

table deptlog(

uname varchar2(20),
action varchar2(20),
dtime date
);
--建立觸發器
--for

each

row 可以觸發多條,當你的語言影響多少條記錄就會觸發多少次

create

or replace trigger trig_dept2

after insert or

delete

orupdate

on dept2/* for each row*/

begin
if inserting then
insert into deptlog values(user, 'insert', sysdate);
elsif updating then
insert into deptlog values(user, 'update', sysdate);
elsif deleting then
insert into deptlog values(user, 'delete', sysdate);
end

if;

end;
select * from dept2;
select * from deptlog;
insert into dept2 values(55, 'software', 'cic');
update dept2 set loc = 'go'

where deptno in(30);

delete dept2 where deptno = 55;
--觸發器
create

or replace trigger trig_emp

after

update

on dept for

each

row

begin
update emp set emp.deptno =: new.deptno where emp.deptno = :old.deptno;
end;
update dept set deptno = 11     where deptno = 10;
select * from emp;
---語句級觸發器
create

or replace trigger trg_ins_dept2

before insert
on dept2
begin
if

user

notin('scott') then

end

if;

end;
禁用、啟用觸發器
alter

trigger tgr_name disable | enable;

alter

table tablename disable all

trigger | enable all

trigger;

oracle plsql中decode 函式用法

在oracle plsql的,decode函式有乙個if then else語句的功能。decode函式的語法是 decode expression search result search result default expression值進行比較。search 是對表達相比的價值。result...

SQL工作常用函式 Oracle PLSQL

整體 示例 select from select airline,substr date,1 4 年,case when date between 2018 01 01 and 2018 08 31 then date else to char to date date yyyy mm dd 364...

oracle plsql 捕獲異常和丟擲異常

在寫oracle儲存過程的時候很多東西放到儲存過程裡面比如一些判斷等,要比在程式邏輯裡面簡單很多,但是也會涉及到捕獲和丟擲一樣的問題。exception when excepttion name1 then when excepttion name2 then when excepttion nam...