1、什么是游標(biāo)
游標(biāo)是一種PL/SQL控制結(jié)構(gòu),可以對SQL語句進(jìn)行顯示控制,便于對表的數(shù)據(jù)逐條進(jìn)行處理
2、游標(biāo)分類
顯示游標(biāo): Declared and named by the programmer
隱式游標(biāo): Declared for all DML and PL/SQL SELECT statements
3、游標(biāo)的屬性:
%FOUND:Evaluates to TRUE if the most recent SQL statement affects one or more rows
%NOTFOUND:和%FOUND相反
%ISOPEN:是一個布爾值,如果游標(biāo)打開,則為TRUE, 如果游標(biāo)關(guān)閉,則為FALSE.對于隱式游標(biāo)而言SQL%ISOPEN總是FALSE,這是因為隱式游標(biāo)在DML語句執(zhí)行時打開,結(jié)束時就立即關(guān)閉。
%ROWCOUNT:Number of records affected by the most recent SQL statement
注意:dbms_output.put_line();這個是不能打印boolean型的,解決方法
if b then
dbms_output.put_line('b=true');
end if;
或者:
declare
b boolean;
begin
b := true;
dbms_output.put_line((case when b then 'true' else 'false' end));
end;
對于null,課先使用NVL()或者decode()處理
顯示游標(biāo):需要手動open和close
例如:
DECLARE
CURSOR mycursor IS
SELECT * FROM dept;
myrecord dept%ROWTYPE;
BEGIN
OPEN mycursor;
FETCH mycursor INTO myrecord;
WHILE mycursor%FOUND LOOP
DBMS_OUTPUT.PUT_LINE(myrecord.deptno||' '||myrecord.dname||' '||myrecord.loc);
FETCH mycursor INTO myrecord;
END LOOP;
CLOSE mycursor;
END;
注意:在進(jìn)行while循環(huán)前,必須先有一個FETCH..INTO操作,否者%FOUND總是返 回false
帶參數(shù)的游標(biāo):
DECLARE
CURSOR mycursor(num varchar2) IS
SELECT * FROM DEPT WHERE deptno=num;
myrecord dept%ROWTYPE;
BEGIN
OPEN mycursor(10);
LOOP
FETCH mycursor INTO myrecord;
EXIT WHEN mycursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('deptNum='||myrecord.deptno||' deptName='||myrecord.dname);
END LOOP;
CLOSE mycursor;
END;
FOR循環(huán)操作游標(biāo)
使用FOR循環(huán)讀取游標(biāo)時,不需要顯示申明變量用于接收結(jié)果,也不需要手動打開和關(guān)閉游標(biāo),例如:
DECLARE
CURSOR mycursor(num varchar2) IS
SELECT * FROM DEPT WHERE deptno=num;
BEGIN
FOR cur IN mycursor(10) LOOP
DBMS_OUTPUT.PUT_LINE('deptNum='||cur.deptno||' deptName='||cur.dname);
END LOOP;
END;
注意:PL/SQL中參數(shù)只需要給出類型,不需要給出長度或精度。
當(dāng)直接將游標(biāo)的值讀取到變量時,變量的個數(shù)應(yīng)與游標(biāo)指向的結(jié)果集的列數(shù)相同。例如結(jié)果集中有兩個列,那么使用FETCH....INTO 時對應(yīng)的變量個數(shù)也應(yīng)該有兩個。
DECLARE
d_no number;
d_name varchar2(10);
CURSOR mycursor(num varchar2) IS
SELECT deptno,dname FROM DEPT WHERE deptno=num;
BEGIN
OPEN mycursor(10);
FETCH mycursor INTO d_no,d_name;
LOOP
DBMS_OUTPUT.PUT_LINE(d_no||' '||d_name);
FETCH mycursor INTO d_no,d_name;
EXIT WHEN mycursor%NOTFOUND;
END LOOP;
CLOSE mycursor;
END;
/
%ROWCOUNT初始值為null,每當(dāng)使用FETCH...INTO從游標(biāo)中取出一條數(shù)據(jù)后, ROWCOUNT的值加1,并不是標(biāo)識結(jié)果集的行數(shù)。
例如:
DECLARE
d_name varchar2(10);
CURSOR mycursor IS
SELECT dname FROM DEPT;
BEGIN
OPEN mycursor;
LOOP
FETCH mycursor INTO d_name;
EXIT WHEN mycursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(mycursor%ROWCOUNT);
END LOOP;
CLOSE mycursor;
END;
結(jié)果集中有4行記錄,輸出結(jié)果為:1 2 3 4
可更新數(shù)據(jù)的游標(biāo)
要想在使用游標(biāo)的同時修改數(shù)據(jù),需要在申明游標(biāo)時加上FOR UPDATE關(guān)鍵字。
例如:
DECLARE
d_name VARCHAR2(20);
CURSOR mycursor IS
SELECT dname FROM dept FOR UPDATE;
BEGIN
OPEN mycursor;
LOOP
FETCH mycursor INTO d_name;
EXIT WHEN mycursor%NOTFOUND;
UPDATE dept SET dname=RTRIM(dname,'_t') WHERE CURRENT OF mycursor;
END LOOP;
CLOSE mycursor;
END;
CURRENT OF+游標(biāo)名:獲取游標(biāo)當(dāng)前所指向的行
RTRIM(dname,'_t'):LTRIM、RTRIM實現(xiàn)字符串過濾(不僅僅去除空格)
隱式游標(biāo):不使用DECLARE顯示申明的游標(biāo)。
例如:
BEGIN
FOR cur IN(SELECT dname FROM dept) LOOP
DBMS_OUTPUT.PUT_LINE(cur.dname);
END LOOP;
END;
1、含有參數(shù)的游標(biāo)
declare
cursor cur_my (mv number) is select * from Person where no<mv;
begin
for tem in cur_my(4) loop
DBMS_OUTPUT.put_line('name:'||tem.name);
end loop;
end;
2、設(shè)置引用游標(biāo)
declare
temp_row Person%rowtype;
type my_type is ref cursor;
cur_my my_type;
begin
open cur_my for 'select * from Person ';
loop
fetch cur_my into temp_row;
exit when cur_my%notfound;
DBMS_OUTPUT.put_line('name:'||temp_row.name);
end loop;
close cur_my;
end;
3、for loop循環(huán)游標(biāo)
DECLARE
v_id Integer;
v_name varchar2(50);
v_age Integer;
cursor cur_mycursor is select id,name,age from Users;
BEGIN
for temp in cur_mycursor loop
v_id :=temp.id;
v_name :=temp.name;
v_age :=temp.age;
dbms_output.put_line('id:'||v_id||'name:'||v_name||'age:'||v_age);
end loop;
/**dbms_output.put_line('所有記錄數(shù):'||cur_mycursor%rowcount||'條!');*/
END;
4、標(biāo)準(zhǔn)化loop循環(huán)游標(biāo)
DECLARE
v_id Integer;
v_name varchar2(50);
v_age Integer;
cursor cur_mycursors is select id,name,age from Users;
BEGIN
OPEN cur_mycursors;
dbms_output.put_line('所有記錄數(shù):'||cur_mycursors%rowcount||'條!');
LOOP
FETCH cur_mycursors INTO v_id,v_name,v_age;
dbms_output.put_line('id:'||v_id||'name:'||v_name||'age:'||v_age);
IF cur_mycursors%NOTFOUND THEN
EXIT;
END IF;
END LOOP;
dbms_output.put_line('所有記錄數(shù):'||cur_mycursors%rowcount||'條!');
CLOSE cur_mycursors;
END;
更多信息請查看IT技術(shù)專欄