一、遊標是什麼
遊標字母理解就是遊動的游標。
用資料庫語言描述:遊標是映射在結果集中的一行資料的實體,有了遊標,使用者就可以訪問結果集中的任意一行資料,將遊標定位到某行後即可對該行資料進行操作,例如提取當前行的資料等。
二、遊標的分類
顯示遊標和隱士遊標
顯示遊標的使用需要4步:
1、聲明遊標
cursor mycursor(vartype number) is
select id from table1
where id=vartype
2、開啟遊標
open mycursor(000627)
3、讀取遊標
fetch mycursor into varno
4、關閉遊標
close mycursor
三、遊標屬性
oracle遊標有4個屬性:
%isopen判斷遊標是否被開啟,如果開啟%isopen等於true,否則等於false
%found、%notfound判斷遊標所在行是否有效,如果有效%found等於true,否則等於false
%rowcount返回當前位置為止遊標讀取的記錄行數
四、樣本
declare
varno varchar(20);
varprice varchar(20);
cursor mycursor(vartype number) is
select emp_no.emp_zc from cus_emp_basic
where com_no=vartype
begin
if mycursor%isopen=false then
open mycursor(000627)
end if;
fecth mycursor into varno.varprice;
while mycursor%found
dbms_output.putlne(varno||'.'varprice);
if mycursor%rowcount=2 then
exit;
end if;
fetch mycursor into varno.varprice;
end;