oracle 遊標
1.自定義(顯式)遊標
set serverout on;
declare cursor cu_emp is select empno,ename,sal from emp;
e_no number;
e_name varchar2(10);
e_sal number;
begin
open cu_emp;
fetch cu_emp into e_no,e_name,e_sal;
while cu_emp%found loop --- 遊標存在時
dbms_output.put_line(‘編號:‘||e_no||‘,姓名:‘||e_name||‘,基本薪資:‘||e_sal);
fetch cu_emp into e_no,e_name,e_sal;
end loop;
close cu_emp;
end;
2.應用被遍歷的的表的列類型
set serverout on;
declare cursor cu_emp is select empno,ename,sal from emp;
e_no emp.empno%type; -- empno列的類型
e_name emp.ename%type;
e_sal emp.sal%type;
begin
open cu_emp;
fetch cu_emp into e_no,e_name,e_sal;
while cu_emp%found loop
dbms_output.put_line(‘編號:‘||e_no||‘,姓名:‘||e_name||‘,基本薪資:‘||e_sal);
fetch cu_emp into e_no,e_name,e_sal;
end loop;
close cu_emp;
end;
3.應用被遍歷表的行屬性
set serverout on;
declare cursor cu_emp is select * from emp; -- declare 可以同時聲明多個變量,用分號隔開。
e emp%rowtype;
begin
open cu_emp;
fetch cu_emp into e;
while cu_emp%found loop
dbms_output.put_line(‘編號:‘||e.empno||‘,姓名:‘||e.ename||‘,基本薪資:‘||e.sal);
fetch cu_emp into e;
end loop;
close cu_emp;
end;
4.遊標的遍歷可以加入條件
set serverout on;
declare cursor cu_emp is select * from emp where sal>2000 and sal<3000;
e emp%rowtype;
begin
open cu_emp;
fetch cu_emp into e;
while cu_emp%found loop
dbms_output.put_line(‘編號:‘||e.empno||‘,姓名:‘||e.ename||‘,基本薪資:‘||e.sal);
fetch cu_emp into e;
end loop;
close cu_emp;
end;
5.隱式遊標(系統聲明好的遊標,取名為sql)
隱式遊標的屬性 返回值類型 意 義
SQL%ROWCOUNT 整型 代表 DML 語句成功執行的數據行數
SQL%FOUND 布爾型 值為 TRUE 代表插入、刪除、更新或單行查詢操作成功
SQL%NOTFOUND 布爾型 與 SQL%FOUND 屬性返回值相反
SQL%ISOPEN 布爾型 DML 執行過程中為真,結束後為假
begin
if sql%isopen then
dbms_output.put_line(‘sql遊標已打開‘);
else
dbms_output.put_line(‘sql遊標未打開‘);
end if;
end;
6.隱式遊標的一些方法
declare e_count number;
begin
select count(*) into e_count from emp;
dbms_output.put_line(‘遊標捕獲的記錄數:‘||sql%rowcount);
end;
declare e_count number;
begin
select count(*) into e_count from emp;
dbms_output.put_line(‘遊標捕獲的記錄數:‘||sql%rowcount);
end;
begin
update emp set ename=‘sb3‘ where empno=111;
if sql%rowcount=1 then
dbms_output.put_line(‘已更新‘);
else
dbms_output.put_line(‘未更新‘);
end if;
end;
begin
update emp set ename=‘sb3‘ where empno=111;
if sql%found then
dbms_output.put_line(‘已更新‘);
else
dbms_output.put_line(‘未更新‘);
end if;
end;
7.強類型動態遊標
declare type emptype is ref cursor return emp%rowtype;
cu_emp emptype;
e_count number;
e emp%rowtype;
begin
select count(*) into e_count from emp where job=‘PRESIDENT1‘;
if e_count=0 then
open cu_emp for select * from emp;
else
open cu_emp for select * from emp where job=‘PRESIDENT‘;
end if;
fetch cu_emp into e;
while cu_emp%found loop
dbms_output.put_line(‘編號:‘||e.empno||‘,姓名:‘||e.ename||‘,基本薪資:‘||e.sal);
fetch cu_emp into e;
end loop;
close cu_emp;
end;
8.弱類型動態遊標(更強大)
declare type customType is ref cursor;
e_count number;
e emp%rowtype;
s salgrade%rowType;
cType customType;
begin
select count(*) into e_count from emp where job=‘PRESIDENT1‘;
if e_count=0 then
open cType for select * from salgrade;
fetch cType into s;
while cType%found loop
dbms_output.put_line(‘等級:‘||s.grade||‘,最低薪資:‘||s.losal||‘,最高薪資:‘||s.hisal);
fetch cType into s;
end loop;
close cType;
else
open cType for select * from emp where job=‘PRESIDENT‘;
fetch cType into e;
while cType%found loop
dbms_output.put_line(‘編號:‘||e.empno||‘,姓名:‘||e.ename||‘,基本薪資:‘||e.sal);
fetch cType into e;
end loop;
close cType;
end if;
end;
oracle 遊標