PL / SQL游标-5

使用记录的示例代码

==========================


declare
cursor c1 is select * from dept;
type drec is record (a dept.deptno%type,
  b dept.dname%type,
  c dept.loc%type);
type ttype is table of drec index by binary_integer;
dr drec;
tt ttype;
i binary_integer:=1;
cnt number;
begin
open c1;
fetch c1 into tt(i);
while (c1%found=TRUE) loop
dbms_output.put_line(tt(i).a||' '||tt(i).b||'   '||tt(i).c);
i := i+1;
fetch c1 into tt(i);
end loop;
cnt := c1%rowcount;
close c1;
end; 
标签光标1

----------------------


 declare
 type tabtype is table of emp%rowtype index by binary_integer;
 rec tabtype;
 cursor c1 is select * from emp;
 counter number:=1;
 begin
 open c1;
 loop
 fetch c1 into rec(counter);
 exit when c1%notfound;
 counter:=counter+1;
 end loop;
close c1;
for i in rec.first..rec.last
loop
dbms_output.put_line(rec(i).empno||' '||rec(i).ename||' '||rec(i).sal);
end loop;
dbms_output.put_line('total record fetched = ' ||rec.count);
end; 
标签光标--4

---------------------------


declare
type my_table_type is table of emp.ename%type
index by binary_integer;
my_table my_table_type;
begin
for i in 1..5 loop
    select ename
    into my_table(i)
    from emp
    where empno=&empno;
end loop;
for i in 1..5 loop
    dbms_output.put_line(my_table(i));
end loop;
end ; 
选项卡-光标3

--------------------------


declare
type tabtype is table of emp%rowtype index by binary_integer;
rec tabtype;
type tabtype1 is table of dept%rowtype index by binary_integer;
rec1 tabtype1;
cursor c1(p number) is select * from emp where deptno=p;
cursor c2 is select * from dept;
counter number:=1;
counter1 number:=1;
begin
open c2;
loop
fetch c2 into rec1(counter1);
exit when c2%notfound;
dbms_output.put_line(rec1(counter1).deptno||' '||rec1(counter1).dname||' '||rec1(counter1).loc);
open c1(rec1(counter1).deptno);
loop
fetch c1 into rec(counter);
exit when c1%notfound;
dbms_output.put_line(rec(counter).empno||' '||rec(counter).ename||' '||rec(counter).sal);
counter:=counter+1;
end loop;
dbms_output.put_line('total record fetched = ' ||rec.count);
counter1:=counter1+1;
close c1;
end loop;
close c2;
end; 
标签光标--4

---------------------------


DECLARE
   TYPE t_type IS TABLE
                  OF emp.empno%TYPE INDEX BY BINARY_INTEGER;
   len BINARY_INTEGER := 0;
   tab t_type;
   CURSOR c1 IS SELECT empno FROM emp ORDER BY empno;
BEGIN
   OPEN c1;
   LOOP
      FETCH c1 INTO tab(len+1);
      EXIT WHEN c1%NOTFOUND;
      len := len + 1;
      EXIT WHEN (c1%ROWCOUNT = 10);
   END LOOP;
   CLOSE c1;
END; 
参数化游标的示例

==========================================


DECLARE
 ob_type   VARCHAR2(6);
 ob_schema VARCHAR2(32);
 ob_name   VARCHAR2(32);
 ob_stat   VARCHAR2(32);
 cursor chk_it (ob_name VARCHAR2) IS SELECT status FROM dba_objects WHERE object_name=ob_name;
BEGIN
  ob_type:='PACKAGE';
 ob_schema:='ACCT_DBA';
 ob_name:= 'CHARGE_FEES';
 IF chk_it%ISOPEN THEN  CLOSE chk_it;
  OPEN chk_it(ob_name);
 ELSE
  OPEN chk_it(ob_name);
 END IF;
 FETCH chk_it INTO ob_status;
 IF ob_status = 'INVALID' THEN
 DBMS_DDL.ALTER_COMPILE(ob_type,ob_schema,ob_name);
 END IF;
 END;
/ 

From: https://bytes.com/topic/oracle/insights/653734-pl-sql-cursor-5-a

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值