plsql存过声明游标_plsql编程学习之游标一
oralce plsql編程的游標(biāo)
游標(biāo)分類
1顯示游標(biāo)
2隱式游標(biāo)
隱式游標(biāo),oracle自動(dòng)管理,不用聲明,打開和關(guān)閉,ORACLE自動(dòng)處理,使用隱式游標(biāo)%FOUND時(shí),需要加上 SQL%FOUND
顯示游標(biāo),需要自己聲明,打開和關(guān)閉,使用%ROWCOUNT屬性時(shí),需要在前面加上游標(biāo)名字 ,student_cur%ROWCOUNT
2聲明游標(biāo)
CURSOR cursor_name is select_statments;
打開游標(biāo)
open cursor_name
讀取數(shù)據(jù)
fetch cursor_name into variable_name,....variable_namen;
關(guān)閉游標(biāo)
close cursor_name;
3游標(biāo)屬性
%ISOPEN
%FOUND
%NOTFOUND
%ROWCOUNT
4游標(biāo)讀取數(shù)據(jù)實(shí)例
select * from students;
set serveroutput on;
declare
v_specialty students.specialty%type;
v_sname students.name%type;
v_dob students.dob%type;
cursor students_cur --聲明游標(biāo)
is
select name ,dob from students where specialty=v_specialty; --游標(biāo)體
begin
v_specialty:='&specialty';
open students_cur; --打開游標(biāo)
dbms_output.put_line('學(xué)生姓名 出生日期');
loop
fetch students_cur into v_sname,v_dob ; --讀取游標(biāo)的數(shù)據(jù)
exit when students_cur%NOTFOUND; --假如沒(méi)有數(shù)據(jù)那么退出
DBMS_OUTPUT.PUT_LINE(v_sname||' '||v_dob);
end loop;
close students_cur; --關(guān)閉游標(biāo)
end;
5根據(jù)游標(biāo)修改當(dāng)前行數(shù)據(jù),語(yǔ)法 update tablename set ....where current of cursor_name;
select * from teachers;
declare
v_title teachers.title%TYPE;
CURSOR teachers_cur
is
select title from teachers for update;
begin
open teachers_cur;
loop
fetch teachers_cur into v_title ;
exit when teachers_cur%NOTFOUND;
case
when v_title='教授' then
update teachers set wage=1.1*wage where current of teachers_cur;
when v_title='高工' or v_title='副教授' then
update teachers set wage=1.1*wage where current of teachers_cur;
else
update teachers set wage=wage+100 where current of teachers_cur;
end case;
end loop;
close teachers_cur;
commit;
end;
6根據(jù)游標(biāo)刪除當(dāng)前數(shù)據(jù) delete from table where current of cursor_name;
select * from students;
declare
v_specialty students.specialty%TYPE;
v_sname students.name%TYPE;
CURSOR students_cur
is
select name,specialty from students for update;
begin
open students_cur;
fetch students_cur into v_sname, v_specialty ;
while students_cur%FOUND loop
if v_specialty ='計(jì)算機(jī)' THEN
delete from students where current of students_cur;
end if;
fetch students_cur into v_sname ,v_specialty;
end loop;
close students_cur;
end;
分享到:
2011-04-12 20:39
瀏覽 2631
分類:數(shù)據(jù)庫(kù)
評(píng)論
總結(jié)
以上是生活随笔為你收集整理的plsql存过声明游标_plsql编程学习之游标一的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 嵌入式Linux安装Python环境,l
- 下一篇: java 运行 出现选择_Eclipse