declare
cursor c_emp is select empno, ename, job, sal from emp where job = 'manager';
v_row c_emp%rowtype;
begin
for v_row in c_emp loop
dbms_output.put_line(v_row.empno || '-' || v_row.ename || '-' || v_row.job || '-' || v_row.sal);
end loop;
end;
或者
declare
cursor c_emp is select empno, ename, job, sal from emp where job = 'manager';
v_row emp%rowtype;
begin
for v_row in c_emp loop
dbms_output.put_line(v_row.empno || '-' || v_row.ename || '-' || v_row.job || '-' || v_row.sal);
end loop;
end;
或者
declare
cursor c_emp is select * from emp;
v_row emp%rowtype;
begin
for v_row in c_emp loop
dbms_output.put_line(v_row.empno || '-' || v_row.ename || '-' || v_row.job || '-' || v_row.sal);
end loop;
end;
declare
cursor c_emp is select * from emp;
v_row emp%rowtype;
begin
open c_emp;
loop
fetch c_emp into v_row;
exit when c_emp%notfound;
dbms_output.put_line(v_row.empno || '-' || v_row.ename || '-' || v_row.job || '-' || v_row.sal);
end loop;
close c_emp;
end;
begin
update emp set ename = 'smith' where empno = 7369;
commit;
dbms_output.put_line('sql%rowcount = ' || sql%rowcount);
if sql%isopen then
dbms_output.put_line('sql%isopen');
else
dbms_output.put_line('not sql%isopen');
end if;
if sql%found then
dbms_output.put_line('sql%found');
else
dbms_output.put_line('not sql%found');
end if;
if sql%notfound then
dbms_output.put_line('sql%notfound');
else
dbms_output.put_line('not sql%notfound');
end if;
exception
when no_data_found then
dbms_output.put_line('no_data_found');
when too_many_rows then
dbms_output.put_line('too_many_rows');
end;
declare
cursor c_emp is select * from emp;
v_row emp%rowtype;
begin
open c_emp;
fetch c_emp into v_row;
while c_emp%found loop
dbms_output.put_line(v_row.empno || '-' || v_row.ename || '-' || v_row.job || '-' || v_row.sal);
fetch c_emp into v_row;
end loop;
close c_emp;
end;
declare
cursor c_emp(p_empno number) is select empno, ename, job, sal from emp where empno = p_empno;
v_row emp%rowtype;
begin
for v_row in c_emp(7369) loop
dbms_output.put_line(v_row.empno||v_row.ename||v_row.sal);
end loop;
end;
create table emp1 as select * from emp;
declare
cursor c_emp is
select e.empno, e.ename, e.job, e.sal, d.deptno
from emp1 e, dept d
where e.deptno = d.deptno for update of e.deptno;
v_row emp1%rowtype;
v_sal emp1.sal%type;
begin
for v_row in c_emp loop
if v_row.sal < 1500 then
v_sal := v_row.sal * 1.2;
elsif v_row.sal < 2000 then
v_sal := v_row.sal * 1.5;
elsif v_row.sal < 3000 then
v_sal := v_row.sal * 2;
end if;
update emp1 set sal=v_sal where current of c_emp;
end loop;
commit;
end;
鎖住e表中滿足條件的行。
或者
declare
cursor c_emp is select empno, ename, job, sal from emp1 for update;
v_row emp1%rowtype;
v_sal emp1.sal%type;
begin
for v_row in c_emp loop
if v_row.sal < 1500 then
v_sal := v_row.sal * 1.2;
elsif v_row.sal < 2000 then
v_sal := v_row.sal * 1.5;
elsif v_row.sal < 3000 then
v_sal := v_row.sal * 2;
end if;
update emp1 set sal=v_sal where current of c_emp;
end loop;
commit;
end;
鎖住所有行。
或者
declare
cursor c_emp is select empno, ename, job, sal from emp1 for update nowait;
v_row emp1%rowtype;
v_sal emp1.sal%type;
begin
for v_row in c_emp loop
if v_row.sal < 1500 then
v_sal := v_row.sal * 1.2;
elsif v_row.sal < 2000 then
v_sal := v_row.sal * 1.5;
elsif v_row.sal < 3000 then
v_sal := v_row.sal * 2;
end if;
update emp1 set sal=v_sal where current of c_emp;
end loop;
commit;
end;
預設情況下當前會話要一直等待對方釋放鎖,使用nowait子句可以避免等待鎖
oracle游標總結
1.利用游標顯示職工號 來廠日期 職位 部門號 部門名 部門所在地 工資所處的等級。declare outempno int outhiredate date outjob varchar 10 outdeptno int outloc varchar 10 outgrade int cursor ...
Oracle 游標的使用總結
declare 定義游標 cursor tcount is select to number to char d create,yyyy as fyear,vc code as fsetid,l fundid as fsetcode,vc name as fsetname,nvl to number...
Oracle游標使用的經驗總結
以下的文章主要是介紹oracle 游標使用,以下就是具體方案的描述,希望在你今後的學習中會有所幫助。首先我們要用到的是select 語句,其用於從資料庫中查詢資料,當在pl sql 中使用select 語句時,要與into 子句一起使用,查詢的返回值被賦予into 子句中的變數,變數的宣告是在del...