十九、Oracle学习笔记:行变量

一、行变量的学习
    说明:行变量可以存储表的一条记录
    格式:变量名 表名%rowtype;
    访问行变量中的字段: 变量名.字段名

--练习:使用行变量输出显示10号部门的员工编号,姓名,职位
  declare
     cursor c is select * from emp where deptno=10;
     row_emp emp%rowtype;--行变量可以存储表的一条记录
  begin
    open c;
       fetch c into row_emp;
       while c%found loop
         dbms_output.put_line(row_emp.empno||'+'||row_emp.ename||'+'||row_emp.job);
         fetch c into row_emp;
       end loop;
    close c;
  end;
  /
    
    
  declare
     cursor c is select * from emp where deptno=10;
     row_emp emp%rowtype;
  begin
    open c;
       loop
         fetch c into row_emp;
       exit when c%notfound;
         dbms_output.put_line(row_emp.empno||'+'||row_emp.ename||'+'||row_emp.job);       
       end loop;
    close c;
  end;
  /
--使用for循环遍历游标
--for循环结构
--说明:for 循环的 in 后除了是数字集合外还可以是游标
--for 变量 in 集合/游标
--修改上面的题
  declare
    cursor c is select * from emp where deptno=10;
    --row_emp emp%rowtype; for循环,可以省略(在遍历行的时候)
  begin
    for row_emp in c loop
      dbms_output.put_line(row_emp.empno||'+'||row_emp.ename||'+'||row_emp.job);
    end loop;
  end; 
  /     

猜你喜欢

转载自blog.csdn.net/qq_38741971/article/details/81429342