oracle 儲存過程 游標

2021-10-10 21:32:05 字數 2661 閱讀 7551

create or replace

procedure "exception3" as

--使用者自定義異常

e_too_high_sal exception; --宣告自定義異常

v_sal employees.salary%type;

begin

select salary into v_sal from employees where employee_id = 100;

if v_sal > 10000 then raise e_too_high_sal;

end if;

exception

when e_too_high_sal then dbms_output.put_line('工資太高');

end;

create or replace

procedure "cursor2"

as-- 定義游標

cursor emp_sal_cursor is select salary,employee_id from employees where department_id = 80;

begin

for c in emp_sal_cursor loop

dbms_output.put_line('employeeid: '|| c.employee_id || ' salary:' || c.salary);

end loop;

end;

create or replace

procedure "cursor3" as

-- 列印出manager_id為100的員工的last_name,email,salary資訊(使用游標,記錄型別)

type emp_record is record(

v_last_name employees.last_name%type,

v_email employees.email%type,

v_salary employees.salary%type

);v_emp_record emp_record;

--定義游標

cursor emp_cursor is select last_name,email,salary from employees where manager_id = 100;

begin

-- 開啟游標

open emp_cursor;

-- 提取游標

fetch emp_cursor into v_emp_record;

--迭代

while emp_cursor%found loop

dbms_output.put_line('last_name:' || v_emp_record.v_last_name ||

' email:' || v_emp_record.v_email ||

' salary:' || v_emp_record.v_salary);

fetch emp_cursor into v_emp_record;

end loop;

-- 關閉游標

close emp_cursor;

end;

create or replace

procedure "cursor4" as

-- 員工工資低於5000 工資增長5% 工資低於10000 增長 3% 工資低於15000 增長2% 其他增長1%

--定義游標

cursor emp_cursor_salary is select salary,employee_id from employees;

v_temp number(4,2);

begin

for c in emp_cursor_salary loop

if c.salary <= 5000 then

v_temp := 0.05;

elsif c.salary <= 10000 and c.salary > 5000 then

v_temp := 0.03;

elsif c.salary <=15000 and c.salary > 10000 then

v_temp := 0.02;

else

v_temp := 0.01;

end if;

update employees set salary = salary * (1+ v_temp) where employee_id = c.employee_id;

end loop;

end;

create or replace

procedure "cursor5" as

--隱式游標:更新指定 員工salary(漲工資10),如果該員工沒有找到,則列印查無此人的資訊

begin

update employees

set salary = salary + 10

where employee_id = 1001;

if sql%notfound then dbms_output.put_line('查無此人');

end if;

end;

oracle儲存過程,游標

oracle儲存過程,游標 2010 07 07 13 01 create or replace procedure p tb task log is 功能 插入任務到任務日誌表 v task start date date v task end date date v sql code numbe...

oracle儲存過程 游標篇

oracle 中cursor用於遍歷臨時表中的查詢結果 1 cursor 型游標 不能用於引數傳遞 create or replace procedure test is cusor 1 cursor is select std name from student where cursor 的使用方...

Oracle儲存過程返回游標

oracle儲存過程返回游標 有倆種方法 一種是宣告系統游標,一種是宣告自定義游標,然後後面操作一樣,引數型別為 in out 或out 1 宣告個人系統游標.推薦 create or replace p temp procedure cur arg out sys refcursor 方法1 be...