使用隱式游標和顯式游標:
1.查詢返回單行記錄時→隱式游標;
2.查詢返回多行記錄並逐行進行處理時→顯式游標
--顯示游標屬性
declare
cursor cur_emp is select * from emp;
row_emp cur_emp%rowtype;
begin
open cur_emp;
fetch cur_emp into row_emp;
while cur_emp%found
loop
dbms_output.put_line(row_emp.empno||'----'||row_emp.ename);
fetch cur_emp into row_emp;
end loop;
close cur_emp;
end;
--使用顯式游標修改資料(給所有的部門經理加薪1000)
declare
cursor emp_cur is
select empno,ename,sal from emp where job='manager' for update;
emp_row emp_cur%rowtype;
begin
open emp_cur;
loop
fetch emp_cur into emp_row;
if emp_cur%notfound then
exit;
else
update emp set sal=sal+1000 where current of emp_cur;
end if;
end loop;
commit;
close emp_cur;
end;
·注意:1、如果游標開啟之前或關閉之後,使用游標屬性,oracle會丟擲乙個invalid_cursor錯誤(ora-01001);
2、如果在第一次fetch後結果集是空的,%found=false,%notfound=true,%rowcount=0;
3、如果使用了bulk collect,那麼%rowcount的值可能不是0或1,實際上他返回的是提取到相關集合的行數。
--游標for迴圈(給所有的部門經理減薪1000)
declare
cursor emp_cur is
select empno,ename,sal from emp where job='manager' for update;
begin
for emp_row in emp_cur
loop
update emp set sal=sal-1000 where current of emp_cur;
end loop;
commit;
end;
--我們可以看到游標for迴圈確實很好的簡化了游標的開發,我們不在需要open、fetch和close語句,不在需要用%found屬性檢測是否到最後一條記錄,這一切oracle隱式的幫我們完成了。
--給經理加薪5000,其他加薪1000
declare
cursor emp_cur is
select * from emp for update;
begin
for emp_row in emp_cur
loop
if emp_row.job='manager' then
update emp set sal=sal+5000 where current of emp_cur;
else
update emp set sal=sal+1000 where current of emp_cur;
end if;
end loop;
end;
Oracle隱式游標和顯式游標
oracle隱式游標和顯式游標,游標是什麼?就是在記憶體開闢的一塊臨時儲存空間。1.1oracle有常用的哪些隱式游標 1.2 oracle隱式游標演示 隱式游標 使用的表為oracle預設自帶的emp表 sql rowcount 影響記錄條數 sql found 是否有滿足條件的記錄 set se...
11顯式游標
1 declare塊中定義顯示游標,並且指定游標讀取的sql語句。2 在begin塊開啟游標。3 從游標中提取資料。4 關閉游標 declare cursor cs user is select from t userinfo order by userid v row t userinfo row...
PL SQL 中顯式游標的使用
一般在pl sql中使用顯式游標有如下的過程 我們以輸出employees表中員工的姓名為例,進行以下的操作 declare v emp first name employees.first name type v emp last name employees.last name type 宣告游...