存儲過程
關鍵字:procedure
存儲過程是PLSQL
的一個方面的應用,而PLSQL
是存儲過程的基礎。
即存儲過程需要用到PLSQL
創建無參存儲過程hello
,無返回值
語法:
create or replace procedure
過程名 as PLSQL程序
set serveroutput on;
create or replace procedure hello
as
begin
dbms_output.put_line('這是我的第一個存儲過程');
end;
刪除存儲hello
,語法drop procedure 過程名
drop procedure hello;
調用存儲過程方式一:exec
過程名
exec hello;
調用過程存儲方式二:PLSQL
程序
begin
hello;
end;
調用過程存儲方式三:Java
程序CallableStatement
接口
創建有存儲過程的raiseSalary
(編號),為7369號員工漲工資10%,演示in的語法,大小寫不敏感
--定義過程
create or replace procedure raiseSalary(p_empno in number)
as
begin
update emp set sal=sal*1.1 where empno=p_empno;
end;
--調用過程
exec raiseSalary(7934);
select * from emp where empno=7934;
創建有參存儲過程findEmpNameAndSalAndJob
(編號),查詢7788號員工的姓名,職位,月薪,返回多個值,演示out
的用法
--定義過程
create or replace procedure findEmpNameAndSalAndJob(p_empno in number,p_name out varchar2,
p_job out varchar2,p_sal out number)
as
begin
select ename,job,sal into p_name,p_job,p_sal from emp where empno=7499;
end;
--調用過程
declare
p_name emp.ename%type;
p_job emp.job%type;
p_sal emp.sal%type;
begin
findEmpNameAndSalAndJob(7499,p_name,p_job,p_sal);
dbms_output.put_line('7734號員工'||p_name||'----'||p_job||'----'||p_sal);
end;
注意: exce 適用于過程無返回值, plsql 調用適用于過程有返回值,不管多少個。
用存儲過程,寫一個計算個人所得稅的功能
--定義過程
create or replace procedure get_rax(sal in number,rax out number)
as
--sal表示收入
--dal表示需要繳稅的收入
bal number;
begin
bal:=sal-3500;
if bal<=1500 then
rax:=bal*0.03-0;
elsif bal<4500 then
rax:=bal*0.1-105;
elsif bal<9000 then
rax:=bal*0.2-555;
elsif bal<35000 then
rax:=bal*0.3-2775;
elsif bal<80000 then
rax:=bal*0.45-5505;
else
rax:=bal*0.45-13505;
end if;
end;
--調用過程
declare
rax number;
begin
get_rax(&sal,rax);
dbms_output.put_line('您需要交的稅'||rax);
end;