1、什麼是游標
游標是一種PL/SQL控制結構,可以對SQL語句進行顯示控制,便於對表的數據逐條進行處理
2、游標分類
顯示游標: Declared and named by the programmer
隱式游標: Declared for all DML and PL/SQL SELECT statements
3、游標的屬性:
%FOUND:Evaluates to TRUE if the most recent SQL statement affects one or more rows
%NOTFOUND:和%FOUND相反
%ISOPEN:是一個布爾值,如果游標打開,則為TRUE, 如果游標關閉,則為FALSE.對於隱式游標而言SQL%ISOPEN總是FALSE,這是因為隱式游標在DML語句執行時打開,結束時就立即關閉。
%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()處理
顯示游標:需要手動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;
注意:在進行while循環前,必須先有一個FETCH..INTO操作,否者%FOUND總是返 回false
帶參數的游標:
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循環操作游標
使用FOR循環讀取游標時,不需要顯示申明變量用於接收結果,也不需要手動打開和關閉游標,例如:
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中參數只需要給出類型,不需要給出長度或精度。
當直接將游標的值讀取到變量時,變量的個數應與游標指向的結果集的列數相同。例如結果集中有兩個列,那麼使用FETCH....INTO 時對應的變量個數也應該有兩個。
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,每當使用FETCH...INTO從游標中取出一條數據後, ROWCOUNT的值加1,並不是標識結果集的行數。
例如:
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;
結果集中有4行記錄,輸出結果為:1 2 3 4
可更新數據的游標
要想在使用游標的同時修改數據,需要在申明游標時加上FOR UPDATE關鍵字。
例如:
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+游標名:獲取游標當前所指向的行
RTRIM(dname,'_t'):LTRIM、RTRIM實現字符串過濾(不僅僅去除空格)
隱式游標:不使用DECLARE顯示申明的游標。
例如:
BEGIN
FOR cur IN(SELECT dname FROM dept) LOOP
DBMS_OUTPUT.PUT_LINE(cur.dname);
END LOOP;
END;
1、含有參數的游標
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、設置引用游標
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循環游標
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('所有記錄數:'||cur_mycursor%rowcount||'條!');*/
END;
4、標准化loop循環游標
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('所有記錄數:'||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('所有記錄數:'||cur_mycursors%rowcount||'條!');
CLOSE cur_mycursors;
END;