Oracl 和 MSSql 的流程控制

来看oracle:

 1 游标,游标的概念在我的理解是数组、是集合、是对象,但是他本身提供了遍历自己的方式

--控制语句 while
create or replace procedure pro_stu_0
as
cursor mycursor is select name,birs from stu;
v_name stu.name%type;
v_birs stu.birs%type;      
begin
 open mycursor;
 fetch mycursor into v_name,v_birs;
 while mycursor%found loop
   dbms_output.put_line(v_name || v_birs);
   fetch mycursor into v_name,v_birs;
 end loop;
 close mycursor;
end pro_stu_0;
--控制语句 loop
create or replace procedure pro_stu_1
as
cursor mycursor is select name,birs from stu;
v_name stu.name%type;
v_birs stu.birs%type;
begin
  open mycursor ;
  loop
  fetch mycursor into v_name,v_birs;
  exit when mycursor%notfound;
  dbms_output.put_line(v_name||v_birs);
  end loop;
  close mycursor;
end pro_stu_1;
--控制语句for
create or replace procedure pro_stu_2
as
cursor mycursor is select name,birs from stu;
begin
 for var_row in mycursor loop
   dbms_output.put_line(var_row.name || var_row.birs);
 end loop;
 close mycursor;
end pro_stu_2;
--ref 游标(动态游标 loop循环)
create or replace procedure pro_stu_3 
as
type mycursor is ref cursor ; --返回 行类型    
v_cursor  mycursor;    
v_row stu%rowtype;    
begin    
  open v_cursor for select * from stu where 1=1;--动态游标 在运行的时候解析参数 静态游标在编译的时候就确定了值   
  loop    
    exit when v_cursor%notfound;    
    fetch v_cursor into v_row;    
    dbms_output.put_line(v_row.name||v_row.birs);    
  end loop;    
  close v_cursor;        
end pro_stu_3;

--ref 游标(动态游标 loop 循环  用游标变量接收)

  create or replace  procedure mypro as
  type mytype_cursor is ref cursor;--声明弱类型游标 强类型:type mytype is ref cursor return emp%rowtype;指定了游标的类型
  mycursor mytype_cursor; --定义变量
  
  cursor mytype is select ename,empno from emp where 1 = 2;
  myrow mytype%rowtype;--1、此处要有rowtype【游标接收】 定义接收数据的游标变量
  
 -- type myrecord is record(ename emp.ename%type,empno emp.empno%type); --2 、复合变量接收
  begin    
    open mycursor for select ename,empno from emp;
    loop
    fetch mycursor into myrow;
    exit when mycursor%notfound;
         dbms_output.put_line(myrow.ename || myrow.empno);
    end loop;
    close mycursor;
      
  end mypro;

--ref 游标(动态游标 while 循环) 

create or replace procedure proc_testCursor
as
type mycursor_type is ref cursor;
mycursor mycursor_type;
v_name emp.ename%type;
begin
    open mycursor for select  ename from emp;
    while mycursor%found loop
      fetch mycursor into v_name;      
      dbms_output.put_line(v_name);
      fetch mycursor into v_name;
    end loop;            
end proc_testCursor;

--/*********ref 动态游标中好像不能用for 循环来遍历************/

解决方法:可以不用游标

create or replace procedure proc_testCursor
as
v_name emp.ename%type;
begin
    for var_row in ( select  ename,empno from emp) loop          
      dbms_output.put_line(var_row.ename||var_row.empno);
    end loop;            
end proc_testCursor;
原文地址:https://www.cnblogs.com/leonkobe/p/3305705.html