一、函式
1. 函式的 helloworld: 返回乙個 "helloworld--!" 的字串
create or replace function helloworld
return varchar2
isbegin
return 'helloworld--!';
end;
執行函式
begin
dbms_output.put_line(helloworld());
end;
2. 定義帶引數的函式: 兩個數相加
create or replace function add_func(a number, b number)
return number
isbegin
return (a + b);
end;
執行函式
begin
dbms_output.put_line(add_func(12, 13));
end;
3. 定義乙個函式: 獲取給定部門的工資總和, 要求, 部門號定義為引數, 工資總額定義為返回值.
create or replace function sum_sal(dept_id number)
return number
iscursor sal_cursor is select salary from employees where department_id = dept_id;
v_sum_sal number(8) := 0;
begin
for c in sal_cursor loop
v_sum_sal := v_sum_sal + c.salary;
end loop;
dbms_output.put_line('sum salary: ' || v_sum_sal);
return v_sum_sal;
end;
執行函式
begin
dbms_output.put_line(sum_sal(80));
end;
4. 關於 out 型的引數: 因為函式只能有乙個返回值, pl/sql 程式可以通過 out 型的引數實現有多個返回值
要求: 定義乙個函式: 獲取給定部門的工資總和 和 該部門的員工總數, 要求: 部門號定義為引數, 工資總額定義為返回值.
create or replace function sum_sal(dept_id number, total_count out number)
return number
iscursor sal_cursor is select salary from employees where department_id = dept_id;
v_sum_sal number(8) := 0;
begin
total_count := 0;
for c in sal_cursor loop
v_sum_sal := v_sum_sal + c.salary;
total_count := total_count + 1;
end loop;
--dbms_output.put_line('sum salary: ' || v_sum_sal);
return v_sum_sal;
end;
執行函式:
delare
v_total number(3) := 0;
begin
dbms_output.put_line(sum_sal(80, v_total));
dbms_output.put_line(v_total);
end;
二、儲存過程
基本上和函式一樣
1. 定義乙個儲存過程: 獲取給定部門的工資總和(通過 out 引數), 要求, 部門號和工資總額定義為引數
create or replace procedure sum_sal_procedure(dept_id number, v_sum_sal out number)
iscursor sal_cursor is select salary from employees where department_id = dept_id;
begin
v_sum_sal := 0;
for c in sal_cursor loop
--dbms_output.put_line(c.salary);
v_sum_sal := v_sum_sal + c.salary;
end loop;
dbms_output.put_line('sum salary: ' || v_sum_sal);
end;
2. 自定義乙個儲存過程完成以下操作:
對給定部門(作為輸入引數)的員工進行加薪操作, 若其到公司的時間在 ? -- 95 期間, 為其加薪 %5 95 – 98 %3 98 -- ? %1
得到以下返回結果: 為此次加薪公司每月需要額外付出多少成本(定義乙個 out 型的輸出引數).
create or replace procedure add_sal_procedure(dept_id number, temp out number)
iscursor sal_cursor is select employee_id id, hire_date hd, salary sal from employees where department_id = dept_id;
a number(4, 2) := 0;
begin
temp := 0;
for c in sal_cursor loop
a := 0;
if c.hd < to_date('1995-1-1', 'yyyy-mm-dd') then
a := 0.05;
elsif c.hd < to_date('1998-1-1', 'yyyy-mm-dd') then
a := 0.03;
else
a := 0.01;
end if;
temp := temp + c.sal * a;
update employees set salary = salary * (1 + a) where employee_id = c.id;
end loop;
end;
Oracle 10g 學習筆記
一 oracle 基礎 oracle的主要結構 一 物理結構 oracle的物理結構基本由一下幾種檔案組成 1.引數檔案 spfilesid.ora 2.控制檔案 v controlfile 3.資料檔案 v datafile 4.日誌檔案 v logfile 啟動過程 1.找到引數檔案 引數檔案中...
Oracle 10g日期時間函式
add months d,n 返回特定日期時間d之後或之前的n個月所對應的日期時間。n為正整數表示之後,n為負整數表示之前 select add months sysdate,5 from dual 輸出 2010 08 26 13 24 28 current date 返回當前會話時區所對應的日期...
oracle10g 解除安裝
1 oracle 10g解除安裝軟體環境 1 windows xp oracle 10g2 oracle 安裝路徑為 d oracle 實現方法 1 開始 設定 控制面板 管理工具 服務停止所有 oracle 服務 2 開始 程式 oracle oradb 10g home1 oracle inst...