光標(biāo)
就是一個結(jié)果集(Result Set)
光標(biāo)的語法
CURSOR 光標(biāo)名 [(參數(shù)名 數(shù)據(jù)類型[參數(shù)名,數(shù)據(jù)類型]..)]
IS SELECT 語句;
一個具體的光標(biāo)
cursor c1 is select ename form emp;
從光標(biāo)中取值
打開光標(biāo):
- open c1; (打開光標(biāo)執(zhí)行查詢)
關(guān)閉光標(biāo):
- close c1; (關(guān)閉光標(biāo)釋放資源)
取一行光標(biāo)的值:
- fetch c1 into pename; (取一行到變量中)
案例
--查詢并打印員工的姓名和薪水
/*
1.光標(biāo)的屬性
%found %notfound
*/
SET SERVEROUTPUT ON
DECLARE
--定義一個光標(biāo)
cursor cemp is select first_name,last_name,salary FROM EMPLOYEES;
--為光標(biāo)定義對應(yīng)的變量
pe_first_name employees.first_name%type;
pe_last_name employees.last_name%type;
psal employees.salary%type;
BEGIN
--打開光標(biāo)
open cemp;
loop
--取一條記錄
FETCH cemp into pe_first_name,pe_last_name,psal;
--exit when 沒有取到記錄;
exit when cemp%notfound;
--打印
DBMS_OUTPUT.PUT_LINE(pe_first_name||' '||pe_last_name||':'||psal);
end loop;
--關(guān)閉光標(biāo)
close cemp;
end;
/