码迷,mamicode.com
首页 > 数据库 > 详细

oracle中的游标

时间:2016-04-18 18:42:17      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

-- 声明游标;CURSOR cursor_name IS select_statement

--For 循环游标
--(1)定义游标
--(2)定义游标变量
--(3)使用for循环来使用这个游标

declare
   cursor yy is
     select * from mytest;
     c_row yy%rowtype;
begin
    for c_row in yy loop
    dbms_output.put_line(c_row.id||-||c_row.name);
    end loop;
end;

执行结果如下图

技术分享

 

--Fetch游标
--使用的时候必须要明确的打开和关闭

 

declare
   cursor yy is
     select * from mytest;
     c_row yy%rowtype;
begin
    open yy;
    loop
    fetch yy into c_row;
    exit when yy%notfound;
    dbms_output.put_line(c_row.id||-||c_row.name);
    end loop;
  close yy;
end;
--使用游标和while循环来显示所有部门的的地理位置(用%found属性)
declare
       --游标声明
       cursor csr_TestWhile
       is
       --select语句
       select  LOC
       from Depth;
       --指定行指针
       row_loc csr_TestWhile%rowtype;
begin
  --打开游标
       open csr_TestWhile;
       --给第一行喂数据
       fetch csr_TestWhile into row_loc;
       --测试是否有数据,并执行循环
         while csr_TestWhile%found loop
           dbms_output.put_line(部门地点:||row_loc.LOC);
           --给下一行喂数据
           fetch csr_TestWhile into row_loc;
         end loop;
       close csr_TestWhile;
end; 

 


--接收用户输入的部门编号,用for循环和游标,打印出此部门的所有雇员的所有信息(使用循环游标)
--CURSOR cursor_name[(parameter[,parameter],...)] IS select_statement;
--定义参数的语法如下:Parameter_name [IN] data_type[{:=|DEFAULT} value]
declare 
      CURSOR 
      c_dept(p_deptNo number)
      is
      select * from emp where emp.depno=p_deptNo;
      r_emp emp%rowtype;
begin
        for r_emp in c_dept(20) loop
            dbms_output.put_line(员工号:||r_emp.EMPNO||员工名:||r_emp.ENAME||工资:||r_emp.SAL);
        end loop;
end;

 

oracle中的游标

标签:

原文地址:http://www.cnblogs.com/yaoyuan23/p/5405297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!