游標的使用

2021-10-05 10:26:58 字數 2733 閱讀 6864

–無參顯游標,%type接收

declare

cursor dept_cursor is select deptno,dname from dept;

v_deptno dept.deptno%type;

v_dname dept.dname%type;

begin

open dept_cursor;

loop

fetch dept_cursor into v_deptno,v_dname;

exit when dept_cursor%notfound;

dbms_output.put_line(『部門號為:』||v_deptno||』,部門名為:』||v_dname);

end loop;

close dept_cursor;

end;

–無參顯游標,%rowtype接收

declare

cursor dept_cursor is select deptno,dname,loc from dept;

v_record dept%rowtype;

begin

open dept_cursor;

loop

fetch dept_cursor into v_record;

exit when dept_cursor%notfound;

dbms_output.put_line(『部門號為:』||v_record.deptno||』,部門名為:』||v_record.dname||』,部門位址為:』||v_record.loc);

end loop;

close dept_cursor;

end;

–無參顯游標,集合接收

declare

cursor dept_cursor is select deptno,dname,loc from dept;

type dept_table_type is table of dept_cursor%rowtype index by binary_integer;

dept_table dept_table_type;

idx number;

begin

open dept_cursor;

loop

idx:=dept_cursor%rowcount+1;

fetch dept_cursor into dept_table(idx);

exit when dept_cursor%notfound or dept_cursor%rowcount>3;

dbms_output.put_line(『部門號為:』||dept_table(idx).deptno||』,部門名為:』||dept_table(idx).dname||』,部門位址為:』||dept_table(idx).loc);

end loop;

close dept_cursor;

end;

–游標for迴圈

declare

cursor dept_cursor is select deptno,dname,loc from dept;

begin

for v_record in dept_cursor loop

exit when dept_cursor%notfound or dept_cursor%rowcount=#

dbms_output.put_line(『部門號為:』||v_record.deptno||』,部門名為:』||v_record.dname||』,部門位址為:』||v_record.loc);

end loop;

end;

–修改游標所在位置

declare

v_deptno emp.deptno%type:=&deptno;

cursor emp_cursor is select empno,sal from emp where deptno=v_deptno for update nowait;

begin

for emp_record in emp_cursor loop

if emp_record.sal<1500 then

dbms_output.put_line(『雇員號為:』||emp_record.empno||』,工資為:』||emp_record.sal);

update emp set sal=1500 where current of emp_cursor;

end if;

end loop;

commit;

end;

–刪除游標所在位置

declare

v_deptno emp.deptno%type:=&deptno;

cursor emp_cursor is select empno,sal,deptno from emp for update nowait;

begin

for emp_record in emp_cursor loop

if emp_record.sal<1500 and emp_record.deptno=v_deptno then

dbms_output.put_line(『雇員號為:』||emp_record.empno||』,工資為:』||emp_record.sal);

delete from emp where current of emp_cursor;

end if;

end loop;

commit;

end;

游標的使用

declare sql varchar 8000 password varchar 200 密碼 tbname varchar 500 資料庫.dbo.表名,如果不指定 dbo.表名,則匯出資料庫的所有使用者表 filename varchar 1000 匯入 匯出路徑 檔名,如果 tbname引數...

游標的使用

游標 cursor 是處理資料的一種方法,為了檢視或者處理結果集中的資料,游標提供了在結果集中一次以行或者多行前進或向後瀏覽資料的能力。我們可以把游標當作乙個指標,它可以指定結果中的任何位置,然後允許使用者對指定位置的資料進行處理。1.游標的組成 游標包含兩個部分 乙個是游標結果集 乙個是游標位置。...

游標的使用

use newsite go object storedprocedure dbo pro cutpoint script date 04 21 2011 10 49 16 set ansi nulls on goset quoted identifier on goalter procedure ...