oracle循环游标

declare
 i int;
begin
 i:=1;
 loop
  dbms_output.put_line(i);
  exit when i=10;
  i:=i+1;
 end loop;
end;

/*
  序列
  create  sequence 序列名;
  序列名.Nextval  取下一个序列
  序列名.Currval  得到当前的序列
*/
create  sequence mySeq;
drop table student;
create table student(
 sid int,
 sname varchar(20)
)

insert into student
values(mySeq.Nextval,'李四');
commit;
select * from student;
select mySeq.Currval from dual;
--删除序列
drop  sequence test;
select test.Currval from dual;
--序列每次递增3,初始值为2
create sequence test start with 2
increment by 3;

declare
 i int:=1;
begin
 while i<10
  loop
   insert into student
values(mySeq.Nextval,'李四'||i);
   i:=i+1;
  end loop;
  commit;--批量插入
end;

create table star(
  sid int
)

begin
  for i in 2..8
   loop
    insert into star values(i);
   end loop;
end;
begin
  for i in reverse 2..8
   loop
    insert into star values(i);
   end loop;
end;
select * from star;


declare
  myException exception;
  pragma exception_init(myException,-2291);
begin
  update student set sname='ceshi' where sid=28789;
  exception when myException then
   dbms_output.put_line('该行不存在');
end;

declare
  myException exception;--定义一个异常变量
begin
  update student set sname='ceshi' where sid=28789;
  if sql%notfound then--判断修改、删除的数据是否被找到
   raise myException;--抛出异常
  end if;
  --捕获异常
  exception when myException then
   dbms_output.put_line('该行不存在');
end;

declare
  myException exception;--定义一个异常变量
begin
  update student set sname='ceshi' where sid=28789;
  if sql%notfound then--判断修改、删除的数据是否被找到
   raise_application_error(-20002,'该行不存在');--抛出异常
  end if;
  --捕获异常
  exception  when others then
   dbms_output.put_line(sqlcode||'该行不存在'||sqlerrm);
end;

--rowid 行id 该行所在的位置
select * from student
where rowid='AAAM7fAABAAAO3qAAA';

declare
 sname student.sname%type;
begin
 select sname into sname from student where sid=28787;
 if sql%found then
  dbms_output.put_line(sname);
 end if;
end;

begin
 update student set sname='ceshi' where sid=28787;
 if sql%notfound then
  dbms_output.put_line('数据未找到');
 end if;
end;

declare
 sn student.sname%type;
begin
 update student
  set sname='ce shi'
   where sid=38787
   returning sname into sn; --<= returning 列名 into 标量变量
 dbms_output.put_line(sn);
end;

declare
type myArr is varray(10000) of varchar2(20);
arr myArr;
begin
 update student
  set sname='ce shi'
  --returning 列名 bulk collect into 集合变量
  --将返回的多行的值放入到集合变量中
  returning sname bulk collect  into arr;
  -- 集合变量名.first  集合的第一个值
  --集合变量名.last  集合的最后一个值
  for i in arr.first..arr.last
  loop
   dbms_output.put_line(arr(i));
  end loop;
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值