[color=green][size=medium]declare[/size][/color]
[color=green][size=medium]case[/size][/color]
[color=green][size=medium]隐式游标[/size][/color]
[color=green][size=medium]显示游标[/size][/color]
[color=green][size=medium]更新游标[/size][/color]
[color=darkblue]-----指向单表的游标-----[/color]
[color=darkblue]------指向关联查询的游标【for update of 列名】------[/color]
[color=olive]/*
更新游标注意点
1.通过游标更新行 for update【of 列名】必须和where current of cursor 配套使用。
2.【of 列名】主要用在指向多表关联的游标中,单表的游标默认锁定。
2.of后是执行要进行锁定表的列【任意一列即可】
3.游标指向关联查询的结果集时,必须指明进行锁定的表【要进行更新的表】,
如果未指定锁定表,将无法更新数据。
4.如果指定了其中A表的锁,更新B表,出错【01410 无效的rowid】。
*/[/color]
[color=green][size=medium]for 游标[/size][/color]
[color=green][size=medium]ref cursor 动态游标[/size][/color]
declare
n_num number;
--变量赋值
v_char varchar2(30):='gmd';
--常量
c_num constant number:=50;
--属性类型数据
c_type t_user.name%type;
r_row t_user%rowtype;
b_boolean boolean:=true;
--自定义数据类型
type my_type is record(
mysuser varchar2(30),
myname varchar2(30)
);
c_my_type my_type;
begin
--对boolean类型数据赋值
if b_boolean=true then
dbms_output.put_line('真值');
else
dbms_output.put_line('假值');
end if;
--第一种赋值方式
n_num:=50;
dbms_output.put_line(n_num);
--第二种赋值方式
select suser into v_char from t_user where suser='test9';
dbms_output.put_line(v_char);
--给%type类型数据赋值
select name into c_type from t_user where suser='test9';
dbms_output.put_line(c_type);
--给%rowtype类型数据赋值
select * into r_row from t_user where suser='test9';
dbms_output.put_line(r_row.suser||r_row.name);
--给自定义数据类型赋值
select suser,name into c_my_type from t_user where suser='test9';
dbms_output.put_line(c_my_type.mysuser||c_my_type.myname);
end;
[color=green][size=medium]case[/size][/color]
declare
n_num number:=15;
begin
--第一种case用法
case n_num
when 10 then
dbms_output.put_line('第一个case');
when 20 then
dbms_output.put_line('第二个case');
else
dbms_output.put_line('没了');
end case;
--第二种case用法
case
when n_num<10 then
dbms_output.put_line('小于10');
when n_num>10 and n_num<20 then
dbms_output.put_line('大于10小于20');
else
dbms_output.put_line('未知数');
end case;
end;
[color=green][size=medium]隐式游标[/size][/color]
declare
begin
update t_user set name='龚帅2' where suser='test9';
if sql%found then
dbms_output.put_line(sql%rowcount);
elsif sql%notfound then
dbms_output.put_line('没有影响');
end if;
commit;
end;
[color=green][size=medium]显示游标[/size][/color]
declare
s_str varchar2(10):='test9';
r_row t_user%rowtype;
cursor my_cursor(ssuser varchar2,nname varchar2) is
select *
from t_user
where suser=ssuser and name=nname;
begin
open my_cursor(s_str,'龚帅');
loop
fetch my_cursor into r_row;
exit when my_cursor%notfound;
dbms_output.put_line(r_row.suser);
dbms_output.put_line(r_row.name);
end loop;
close my_cursor;
end;
[color=green][size=medium]更新游标[/size][/color]
[color=darkblue]-----指向单表的游标-----[/color]
declare
cursor mycursor is
select * from t_user for update;
begin
for myrow in mycursor
loop
dbms_output.put_line(myrow.name);
update t_user set name=myrow.name||'1' where current of mycursor;
end loop;
commit;
end;
[color=darkblue]------指向关联查询的游标【for update of 列名】------[/color]
[color=olive]/*
更新游标注意点
1.通过游标更新行 for update【of 列名】必须和where current of cursor 配套使用。
2.【of 列名】主要用在指向多表关联的游标中,单表的游标默认锁定。
2.of后是执行要进行锁定表的列【任意一列即可】
3.游标指向关联查询的结果集时,必须指明进行锁定的表【要进行更新的表】,
如果未指定锁定表,将无法更新数据。
4.如果指定了其中A表的锁,更新B表,出错【01410 无效的rowid】。
*/[/color]
declare
cursor mycursor is
select name,remark from t_user a,user_role b
where a.suser=b.suser for update of name;
begin
for myrow in mycursor
loop
dbms_output.put_line(myrow.name);
dbms_output.put_line(myrow.remark);
--指定了锁定的表,即可进行更新
update t_user set name=myrow.name||'a',remark=myrow.remark||'a' where current of mycursor;
--如果指定了其中A表的锁,更新B表,出错【01410 无效的rowid】
--update t_user_address set addressname='qwe' where current of mycursor;
end loop;
commit;
end;
[color=green][size=medium]for 游标[/size][/color]
declare
cursor my_cursor is
select * from t_user;
begin
for my_row in my_cursor
loop
dbms_output.put_line(my_row.name);
end loop;
end;
[color=green][size=medium]ref cursor 动态游标[/size][/color]
declare
s_str varchar2(10):='test9';
v_sql varchar2(100);
r_row t_user%rowtype;
type my_cursor is ref cursor;
r_cursor my_cursor;
begin
v_sql:='select * from t_user where suser=:ssuser';
open r_cursor for v_sql
using s_str;
loop
fetch r_cursor into r_row;
exit when r_cursor%notfound;
dbms_output.put_line(r_row.name);
end loop;
close r_cursor;
end;
3962

被折叠的 条评论
为什么被折叠?



