oracle函數、包、變量的定義和使用、重點”結構體和數組”


函數

實例1:輸入雇員的姓名,返回該雇員的年薪

create function fun1(spName varchar2) return number is yearSal number(7,2);
begin 
select sal*12+nvl(comm,0) into yearSal from emp where ename=spName;
return yearSal;
end;
/

在sqlplus中調用函數
var income number
call annual_incomec('SCOTT') into: income;
print income

同樣我們可以在java程序中調用該函數
select annual_income('SCOTT') from dual;
可以通過rs.getInt(1)得到返回的結果

 


包用於在邏輯上組合過程和函數,它由包規范和包體兩部分組成。

實例1 用create package命令來創建包
create package mypackage is 
procedure update_sal(name varchar2, newsal number);
function annual_income(name varchar2) return number;
end;
包的規范只包含了過程和函數的說明,但是沒有過程和函數的實現代碼,包體用於實現包規范中的過程和函數

實例2 建立包體使用命令create package body的命令
create package body mypackage is procedure update_sal(name varchar2, newsal number)
is 
begin 
update emp set sal=newsal where ename=name;
end;

function annual_income(name varchar2)
return number is
annual_salary number;
begin
select sal*12+nvl(comm,0) into annual_salary from emp where ename=name;
return annual_salary;
end;

end;

實例3 如何調用包的過程和函數
當調用包的過程和函數時,在過程和函數前需要帶有包名,如果要訪問其他方案的包,還需要在包名前加方案名
call mypackage.update_sal('SCOTT',1500);

觸發器
觸發器是指隱含的執行的存儲過程,當定義觸發器時,必須要指定觸發的事件和觸發的操作,常用的觸發事件包括insert,update,delete語句,而觸發操作實際就是一個pl/sql
塊,可以使用create trigger來建立觸發器

變量的定義和使用
在編寫pl/sql程序時,可以定義變量和常量,在pl/sql程序中包括有:
1標量類型(scalar)
定義一個變長字符串: v_ename varchar2(10);
定義一個小數並給初始值: v_sal number(6,2):=5.4;
定義一個布爾便令不能為空初始值為false:v_valid boolean not null default false
定義一個日期類型的數據: v_date date;

在定義好變量后,就可以使用這些變量了,這里需要說明的是pl/sql塊為變量賦值不同於其他的編程語言,需要在等號前加冒號。

案例1:以輸入員工號,顯示雇員姓名,工資,個人所得稅(0.03)為例。
declare 
c_tax_rate number(3,2):=0.03;
v_name varchar2(5);
v_sal number(7,2);
v_tax_sal number(7,2);
begin
select ename,sal into v_ename,v_sal from emp where emp=&no;
--計算所得稅
v_tax_sal:=v_sal*c_tax_rate;
dbms_output.put_line('姓名是:'||v_ename||' 工資:'||v_sal||' 交稅':v_tax_sal)
end;

2 增強版標量%type
在正常定義標量時字符需要指定大小,太大了浪費空間,可以采用:
v_ename emp.ename%type;
這是v_ename的類型和大小和表emp中字段ename的類型和大小保持一致性。

3 復合變量(composite)
用於存放多個值的變量,主要包括這幾種:pl/sql記錄、pl/sql表、嵌套表、varry

(1)pl/sql記錄;類似高級語言中的結構體

declare
type emp_record_type is record(
v_name varchar2(5),
v_sal number(7,2));

sp_record emp_record_type;

begin 
select ename,sal into sp_record from emp where empno=22334;
dbm_output.put_line('員工名:'||emp_record.name);
end;

 

(2) pl/sql表:相當於數組

declare 
--定義了一個pl/sql表類型sp_table_type,該類型是用於存放emp.ename%type
--index by binary_integer表示下標是整數
type sp_table_type is table of emp.ename%type index by binary_integer;
--定義了一個sp_table變量
sp_table sp_table_type;
begin
select ename into sp_table(0) from emp where empno=7788;
dbms_output.put_line('員工名:'||sp_table(0));
end;

如果上述中select語句將where語句去掉,也就是返回了很多數據。

3參照變量 : 高級數組
參照變量是指用於存放數組值的指針變量,通過使用參照變量
(1)參照變量-ref cursor游標變量
使用游標時,當定義游標時,當定義游標時不需要指定相應的select語句,但是當使用游標時需要指定select語句,這樣一個游標就與一個select語句結合了。

實例1:編寫一個塊,可以輸入部門號,顯示該部門所有員工的信息
declare 
--定義游標類型
type sp_emp_cursor is ref cursor;
--定義一個游標變量
test_cursor sp_emp_cursor;
v_ename emp.ename%type;
v_sal emp.sal%type;

begin
--把test_cursor和一個select結合
open test_cursor for select ename,sal from emp where deptno=&no;
--循環取出
loop
fetch test_cursor into v_ename,v_sal;
--判斷退出條件
exit when test_cursor%notfound;
dbms_output.put_line(v_ename||' '||v_sal);
end loop;

end;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM